Intro to Deno Express JS
Tags: deno
Published: 2020-06-12 04:36:45

<p>Express JS is one of the best application interface for Node.JS, Express provide middleware over our raw application so its user can build routes, middleware, and much more. Express also gives a whole new aspect of development by building MEAN, MERN, MEVN stacks.

So can Express JS be used in Deno 🧐?
Deno third-party module Deno Train has implemented some of the features which resemble the Express in node. So without wasting much time let’s get started… 

First we need to create app.ts, after creating let’s import the Deno Train using import syntax as
</p>
<pre>

// Importing Deno Train
import { Application, Router } from "https://deno.land/x/denotrain@v0.5.0/mod.ts";

</pre>

<p>

After importing we need to initialize our application class and router class so we can implement our app as well as create routing path which perform according to user request.

</p>

<pre>

// Initialization of App and Router

const app = new Application();

const router = new Router();

</pre>

<p>

Now we need to set our router to perform action, we can set which type of request we send to our server like (GET,POST,DELETE or etc).

</p>

<pre>

// Setting up get request in base path

router.get("/get", ({req,res})=> {
 // Implement your logic
 return "Hello World Deno is awesome 👏 "
});

router.post("/post", ({req,res})=> {
 // You can request submitted form data by just requesting the body
 const formData = req.body;
 return "This is post submit any thing you want 🍉"
});

router.delete("/delete", ({req,res})=> {
 // Implement your logic
 return {"msg": "Your item has been deleted ❌"} // Returning a json
});

router.patch("/get", ({req,res})=> {
 return "Your application is updated"
});


</pre>

<p>

So this how we can handle the requests using our Deno Train not lets make this router available to application as we need to pass it as a middleware then we need to start our app.

</p>

<pre>

// Pushing our router inside our application as a middleware

app.use("/url-path", router);
// Now let our app to run on port 3000 by default we can change if we want

await app.run();

</pre>

<p>

Now open up Post Man and tries to send each request and check your application is working or not. Use this url “localhost:3000/url-path/get” for the get request we can similarly apply for Delete, Update and Post request.

Our final application will look something like this.

</p>

<pre>

// Importing Deno Train
import { Application, Router } from "https://deno.land/x/denotrain@v0.5.0/mod.ts";

// Initialization of App and Router

const app = new Application();

const router = new Router();

// Setting up get request in base path

router.get("/get", ({req,res})=> {
 // Implement your logic
 return "Hello World Deno is awesome 👏 "
});

router.post("/post", ({req,res})=> {
 // You can request submitted form data by just requesting the body
 const formData = req.body;
 return "This is post submit any thing you want 🍉"
});

router.delete("/delete", ({req,res})=> {
 // Implement your logic
 return {"msg": "Your item has been deleted ❌"} // Returning a json
});

router.patch("/get", ({req,res})=> {
 return "Your application is updated"
});
// Pushing our router inside our application as a middleware

app.use("/url-path", router);
// Now let our app to run on port 3000 by default we can change if we want

await app.run();

</pre>

<p>

So we finally implement Express like application inside Deno using Deno Train hope your may like it. And if we make our application using Deno, Mongo, Express and Javascript Front-End library then their acronym will be MERD, MEVD & MEAD.

</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z5p9oa84nnm3c6xxvjkv.gif)