Loading

Deno - A First Look

Deno - A First Look

Deno! What an interesting name. A new thing which is in hiped these days. Curious to know more...lets jump into this

A secure Javascript and Typescript runtime combined, simplified and named as DENO. It’s built in RUST programming language, TOKIO assynchronous runtime for Rust and uses Chrome’s V8 engine.

Lets install it

Deno installation is pretty simple. See below. if you are in MacOS or Linux

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

Then copy the export line and add it to your ~/.bashrc or ~/.bash_profile. Open a new terminal and

deno --version

If you are in Windows, just open the power shell and run the following

iwr https://deno.land/x/install/install.ps1 -useb | iex

Now

deno --version

Your First Deno Code

Deno supports javascript & typescript both so you can try the following... Open any editor of your choice, create a file, say, “welcome.js” or “welcome.ts” and write this code:

console.log(“Hello World!”);

Now, its time to execute the code and let see what is happening.

deno run welcome.ts

Hello World!

deno run welcome.js

Hello World!

Permissions in Deno

One of the important and outstanding features of Deno is Secuirty and how it is implemented. Deno doesn’t have any access to your file system, network. You have to allow deno explicitly while running a program.

Let’s see what it means by this example.. Like, node let’s create a simple server in Deno...

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

Lets understand the lines one by one. Ok?

At the very 1st line, we have 2 new things to understand. Number 1, we have an importing statement. Meaning we don’t need to install anything for this program to execute, like we did for Node. So there will not be any node_modules in our local system.

Number 2, it actually importing an HTTP server from Deno’s library via URL. Meaning we have to use your network to execute this line. Right? Without a network you won’t be able to access anything via internet. Aha! What a beauty. Without your permission Deno will not going to access any URL via your network. Sounds great! Isn’t it? Now coming to the 2nd line, here we create a server and alow a port to act upon. Then, it will running a loop to serve requests from the server. One interesting thing we can find here is await. Pretty simple right! And by the way save it by any name, let say server.ts or server.js whatever you want. Now we will execute this program by deno run server.ts

After running this program you will see an output like...

error: Uncaught PermissionDenied: network access to "0.0.0.0:3000", run again with the --allow-net flag
    at unwrapResponse (rt/10_dispatch_json.js:25:13)
    at sendSync (rt/10_dispatch_json.js:52:12)
    at opListen (rt/30_net.js:33:12)
    at Object.listen (rt/30_net.js:204:17)
    at serve (server.ts:287:25)
    at server.ts:2:11

Here you can see the error it clearly shows the program will not be able to access the URL via internet. Now we need to apply the security options. Here we will use --allow-net flag. By this flag it will allow Deno to access the network to reach that URL. So, the whole command will be..

deno run --allow-net server.ts

Now you click on http://localhost:3000 it will show you the message Hello World Lets find out the flags Deno will use for permissions --allow-env allows environment access --allow-net allows network access --allow-plugin allows plugins access --allow-read allows file system read access --allow-run allows running subprocesses --allow-write allows file system write access --allow-all allows all permissions

Now Deno does one interesting thing, it first loads the modules from the URL and it will cache it in your system. Interesting isn’t it. Next time you can reload the module from the cache. You just pass –reload flag.

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

Summary

We are now happy to see the first look of Deno. Is it useful to you? If so, please leave a comment at info@codinghub.net and share this article as much as you can. This is just the begining. There are lot more to come. Stay connected with us. Thank you very much!

We are on Facebook, Twitter, LinkedIn, Medium, Quora, Instagram, etc.

By the way, want to learn more about Deno? Just check out the official Deno documentation.

Related Articles