7th Jul 2020

Exploring deno.


Written By, Nikhil Savaliya


Have you heard about Deno? The new alternative to Node?
Deno is a secure runtime for Typescript and Javascript that uses v8 and built-in Rust. (Deno is an anagram of Node)
Deno doesn’t use the NPM/Yarn package manager, instead, it is a loading module by referring to file path or URLs.
Instructions for installation -> deno.land.
Highlights of Deno:

  • Deno is secure by default unless you provide explicitly access to net or files
  • Deno dies on uncaught errors.
  • All async actions in Deno return a promise (You can use await anywhere 😜).
  • Scripts written in deno can be bundled into a single Js file.

There is no doubt about Node’s maturity, but deno is a thing to try

Excited to drive in with deno? Let’s see a few real-life examples we usually deal with Nodejs and how we can achieve that in deno.

Creating HTTP server

While working with the servers we usually deal with middlewares and routers so oak 🌳 is inspired by Koa and middleware routers inspired by @koa/router.
So hope you are ready with installation, create a file named server.ts

The Application class wraps the serve() function. It has two methods: .use() and .listen().
Same as Node Middleware is added via the .use() method
Let’s achieve Hello world,
Add new file router.ts and controllers/index.ts
The Router class produces middleware which can be used with an Application to enable routing based on the pathname of the request.

With a response we have properties like body, headers, status, type, and a method for redirection (redirect).

That’s it for hello world.
Need to run the server.ts file.
deno run –allow-net server.ts
Read about permission here –allow-net

Route to http://localhost:5000 and you will receive Hello world.

Serving simple HTML files and template parsing

Update the server.ts file with below code,
We can pass view engine as middleware, and update configuration like,
viewRoot will contain our static files folder name and viewExt will allow us to pass an extension of files like .html, .ejs or any other like Denjuck or Handlebars

As well we have same root (“/”) route in controllers/index.ts file lets update that to serve HTML file,

Restarting the server and reloading the browser, didn’t work right?

Do you know what we missed?
We have used the read folder in our application and deno will not read until we provide a flag to read the files from our system.
So again run the server with deno run –allow-net –allow-read server.ts

Now it will work like charm 😜.

We just served HTML files, what about EJS?
Let’s update the config in server.ts,

Create a new file as index.ejs,

Now to render EJS file everything is same expect we have to dynamic data in EJS file so lets update controller/index.ts file

Now restart the server and you will see the output with EJS rendered.

Grab the code in case you missed something or not able to figure it out.

Too much time is wasted in restarting the server in development right? you can try denon.

Let’s keep exploring and wait for the upcoming recipe with me here for advanced concepts in deno.

Written By,

MEAN stack developer at Yudiz Solutions Pvt. Ltd