Let’s meet Deno!!!

Soumarshi Biswas
Nggawe Nirman Tech Blog
4 min readSep 11, 2020
Deno v1.0

Deno is a new JavaScript /Typescript runtime framework developed by Ryan Dahl 3 months back in May 2020. Yes, you heard it right…the same person who developed Node.

According to Ryan Dahl, he was worried about the security that Node offered and wanted to create something more secure So comes Deno with all the new features of Javascript with inbuilt security features.

Let's dig deep.

Features of Deno

According to the official website, Deno provides the following features out of the box.

  • It’s built on V8, Rust, and Tokio (Rust runtime for writing Async operations).
  • Secure by default. No resources are enabled by default. You need to explicitly give permissions.
  • Ships a single executable file.
  • Has multiple inbuilt utility functions for the ease of the developer.
  • Provides standard modules maintained by the Deno dev team
  • Unlike Node, Deno doesn't use any NPM modules. Instead, Deno uses third party packages using browser compatible URL. These are maintained by the Deno community.

Now let's install Deno.

Open your terminal and copy/paste the following command to install Deno.

curl -fsSL https://deno.land/x/install/install.sh | sh

Check version.

deno --version

Run a hello world program directly using the deno repo browser compatible URL from your terminal.

deno run https://deno.land/std/examples/welcome.ts

You should see the following response.

Welcome to Deno

One cool feature of Deno is it doesn't have permission on any resources on the system you run. You need to give permission to run your program.

Let’s learn it by building our first web server in Deno.

Deno Server

Copy the below code in a file with .ts extension like “app.ts”.

import { serve } from “https://deno.land/std/http/server.ts";
const s = serve({ port: 3000 });
console.log("http://localhost:3000/");
for await (const req of s) {
req.respond({ body: “Hello World\n” });
}

So, the first line shows the main difference between Node and Deno. No package import or install required. You can directly refer to the HTTP server from the deno standard library URL and serve on the port 3000. Notice that Deno provides top-level await functions which means you do not need an async function to call await.

Once you are done you can run the app.ts using the below command allowing permission to access the network on your system.

deno run --allow-net app.ts

Now open your browser and navigate to localhost:3000, you should see a message in the browser.

Here are the flags that allow Deno to allow permissions depending on your use case.

–allow-env allow environment access
–allow-net= allow network access
–allow-plugin allow plugins access
–allow-read= allow file system read access
–allow-run allow running subprocesses
–allow-write= allow file system write access

You can allow all permissions using the below flag instead of specifying it individually

–allow-all allow all permissions

For the first time execution, it will download all the modules from the 3rd party and standard libraries and store it in its cache. You can reuse the same by adding a reload flag instead of downloading them again to have a faster boot up time for your server.

deno run --reload --allow-net app.ts

Deno Runtime

Deno’s runtime documentation can be found here.

It provides information on the in-built functions and the Rust plugins that Deno provides for the ease of your development.

The basic runtime documentation for Deno can be found on doc.deno.land.

Deno Standard modules

Similar to Node, Deno also provides some core packages called Standard modules which are developed and maintained by the Deno dev team

Deno Standard Modules

Deno third party module

Deno's third-party modules can be found here.

These are maintained by the Deno community which is growing stronger each day.

Hello World! web app using 3rd party modules

Consider the code below which is similar to Express Node Framework.

import opine from "https://deno.land/x/opine@0.21.2/mod.ts";
const app = opine();
app.get("/", function (req, res) {
res.send("Hello World!");
});
app.listen(3000);
console.log("Opine started on port 3000");

Run the code and browse on http://localhost:3000

deno run --allow-net app.ts

You can also use oka or aqua for a similar experience like the Express framework.

Will cover more on them in upcoming blogs.

Conclusion

Deno is certainly very easy to pick up if you already know NodeJS frameworks or are familiar with any javascript frameworks.

Setup is easy and with all the different 3rd party modules coming up, it will surely give you the same feel and comfort of development that you used to get with NodeJS. It is a really good alternative for building robust and secure applications.

With new modules being developed in Deno by the community it poses an interesting future. Let’s wait and see !!! Happy coding…

--

--