Deno REST API with CRUD Operation using MongoDB : 1 Setting-up Project
Tags: deno, webdev
Published: 2020-06-03 16:12:27

<p>

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.

</p>

<pre>

./
|-index.html
|-app.ts
|-models/
   |-user.ts
   |-mongo.ts
|-controllers/
   |-users.ts
|-routes/
   |-users.ts

</pre>

<p>

Let type some of our boiler plate codes inside app.ts.

</p>

<pre>

// 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}`);


</pre>

<p>

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.

</p>

<pre>

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"));

</pre>

<p>

As our route has been prepared let import these routes inside our app.ts file so our file finally look like.

</p>

<pre>

//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}`);

</pre>

<p>

Now our app.ts has been setup let check our application is running our for checking we need to use <a href="https://medium.com/recoding/creating-hot-reload-or-live-reload-in-deno-app-using-denom-a62c58e0b878">Denon</a> for live loading of app. Type the command

</p>

<pre>

denon run --allow-net --allow-flag --allow-read --allow-write app.ts

</pre>

<p>

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.

</p>

{% youtube FR6vgRoa6lc%}