Haaris Iqubal
Posted on June 3, 2020
This tutorial is focusing creating a simple REST API using Deno. By the latest introduction of Deno one of the most interesting thing to do with it is to perform CRUD operation with your Deno app. As Deno doesn't have any database associated with it so we need use other databases like SQL, PostgreSQL, SQLite or MongoDB. For the sake of this example we gonna use MongoDB. So without further ado let's setup our project. For our Deno app we are going to Oak which provides us app layer while Mongo Module which provides use interfacing command for CRUD in mongoDB. Our RestAPI will support MVC (Model View Controller) architecture. So our file structure gonna look like this. View will not be implemented in this tutorial, as this is only as RestAPI you can implement in your iOS, Android, Windows or macOS apps easily.
./ |-index.html |-app.ts |-models/ |-user.ts |-mongo.ts |-controllers/ |-users.ts |-routes/ |-users.ts
Let type some of our boiler plate codes inside app.ts.
// Requiring modules import { Application, Router,send } from "https://deno.land/x/oak/mod.ts"; // Setting ENV variables const env = Deno.env.toObject(); const PORT = env.PORT || 8000; const HOST = env.HOST || 'localhost'; // Initiate const app = new Application(); const router = new Router(); // Here we can setup our logger and timer app.use(async (ctx,next)=> { await next(); const rt = ctx.response.headers.get("X-Response-Time"); console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`); }) app.use(async (ctx,next)=> { const start = Date.now(); await next(); const ms = Date.now(); ctx.response.headers.set("X-Response-Time",`${ms}ms`); }) // Starting our server console.log(`App is listening to ${PORT} PORT`); app.listen(`${HOST}:${PORT}`);
Now we need to create our Routes so inside our routes folder open users.ts file. Inside it we need to pass our request we want it might be “GET”, “POST”, “DELETE” or any other.
import {Router} from 'https://deno.land/x/oak/mod.ts'; const router = new Router({prefix: '/user'}); router .get('/allUsers', console.log("All user Path")) .post('/sign-up', console.log("Sign-Up User")) .post('/sign-in',console.log("Sign-In User")) .delete('/delete/:userId', console.log("Delete User")) .patch('/update/:userId', console.log("Update User"));
As our route has been prepared let import these routes inside our app.ts file so our file finally look like.
//Importing our modules import {Application} from 'https://deno.land/x/oak/mod.ts'; import UserRoute from './routes/users.ts'; // Setting ENV variables const env = Deno.env.toObject(); const PORT = env.PORT || 8000; const HOST = env.HOST || 'localhost'; //Setting our application and router const app = new Application(); //Here we can setup our logger and timer app.use(async (ctx,next)=> { await next(); const rt = ctx.response.headers.get("X-Response-Time"); console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`); }) app.use(async (ctx,next)=> { const start = Date.now(); await next(); const ms = Date.now(); ctx.response.headers.set("X-Response-Time",`${ms}ms`); }) //Now we can pass UserRoute as middleware to app app.use(UserRoute.routes()); app.use(UserRoute.allowedMethods()); // Starting our server console.log(`App is listening to ${PORT} PORT`); app.listen(`${HOST}:${PORT}`);
Now our app.ts has been setup let check our application is running our for checking we need to use Denon for live loading of app. Type the command
denon run --allow-net --allow-flag --allow-read --allow-write app.ts
Now open PostMan check our API we have created is working or not. Inside Post Man try to send a “GET” request at “localhost:8000/users/allUsers”. If you get comment on your console you have written above then our path is working. So we have created our project structure for and implemented boilerplate. We gonna implement DB Setup and our Controller in our next post until then stay tune.
Posted on June 3, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.