DEV Community

loading...
Cover image for Intro to Deno Express JS
Recoding

Intro to Deno Express JS

haarisiqubal profile image Haaris Iqubal Originally published at Medium ・3 min read

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


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

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.


// Initialization of App and Router

const app = new Application();

const router = new Router();

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).


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


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.


// 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();

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.


// 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();

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.

Alt Text

Discussion (0)

pic
Editor guide