blog
blog
blog
blog
blog

Getting Started With Deno

Deno, Deno, Deno! It has been much of a buzz in the past few months. So what exactly is Deno? Deno is a secure runtime for JavaScript and TypeScript. In this article, we will talk about Deno and get started with our first Deno code. So, let’s start with Deno.

Flutter

What is Deno?

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. So, is it yet another JavaScript runtime Like Node? Yes! But, more like Node 2.0. Yes, one thing to notice here is, it is not just a runtime for JavaScript, but also for Typescript, Well that’s an added benefit! Deno is created by Ryan Dahl, the same person who created Node and aims to be a productive and secure scripting environment for the modern programmer.

So, the question arises, Why did Ryan make Deno while Node is already there? Well, according to Ryan, there were some design flaws in Node. Thus, he made Deno, a new and better runtime for JavaScript by addressing the flaws in Node. To dive deep into that part, you must watch the following video and hear it from the creator himself,

Flutter

Watch Now - 10 Things I Regret About Node JS

Why Deno?

Let’s have a look at some top features of Deno that attracts a lot of developers,

  • Deno is secure by default. This means no file, network, or environment access unless explicitly enabled.
  • It supports TypeScript out of the box.
  • Deno ships only a single executable file.
  • It has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).

Will Deno Kill Node?

Well, this has been one of the most talked topics about Deno since its release. But, the simple answer is No! Deno won’t kill Node. Over the years, Node has evolved and has a huge fanbase out there, also Node is a giant when it comes to JavaScript development. Thus, there are no chances Deno will kill Node, at least as of now. Deno is quite new, released just in May 2020, and will take time to be accepted by the industry. Thus, Deno can be an alternative to Node, but not a replacement!

Let’s about Node VS Deno

As a developer, you should know similarities and differences in Deno and Node, irrespective of whether you want to get hands-on with it or not!

Similarities,

  • Deno and Node, both are developed upon the V8 Chromium Engine
  • Both are great for developing server-side with JavaScript

Differences,

  • Deno does not use npm, instead, it uses modules referenced as URLs or file paths
  • Deno does not use package.json in its module resolution algorithm.
  • Deno uses modern ECMAScript features in all its API and standard library, while Node.js uses a callbacks-based standard library and has no plans to upgrade it.
  • Deno is written in Rust and TypeScript while Node is written in C++ and JavaScript.
  • Deno requires explicit permissions for the file, network, and environment access, while that is not the case with Node

Oh, that’s enough for theory! Now before we end, let’s install Deno and create a simple HTTP server using Deno.

Deno Installation

Installing Deno is not much brainer and is pretty simple.

For Mac/Linux,

Hit the below command in your terminal to install Deno,

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

For Windows,

Hit the below command in your windows Powershell to install Deno,

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

Well, that’s pretty much it!😉
Now, let’s check the installation. To test your installation, hit the command deno --version. If this prints the Deno version to the console, then the installation was successful.

Our First Deno Application

Once we are done with the installing, we are ready to create the very first application in Deno. Of course, it has to be the “Hello World” program. So, let’s create a simple Deno program that will print Hello World to the output screen.

Create a file, Hello.ts, and write the below code in it,

                                console.log(“Hello World”);
                            

Looks familiar? Yes, it is a simple JavaScript Code. Deno is a JavaScript and TypeScript runtime. This means the code will be simple JavaScript or TypeScript code with a pinch of Deno. So, now let[‘s run the program. Hit the below command in your terminal to run the file,

                                deno run Hello.ts
                            
                                Output: 
                                    Hello World
                                    
                            

Pretty simple, right?
deno run is the command that is used to execute files in Deno. Now, let’s move ahead, and create a simple HTTP server in Deno

HTTP Server using Deno

Let’s create a simple HTTP server that will display ‘Hello World’ on our web page. Create a file called app.ts and write the below code in it,

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

                                
                            

Now, let’s breakdown the code,
The first line imports the serve function from the http/server module. Here one thing to notice is we don’t need to install it using npm. Just import will do the job.
This code imports the serve function from the http/server module. Next, We proceed to instantiate a server calling the serve() function passing an object with the port property.
Then we run this loop to respond to every request coming from the server. Here, we use the await keyword without having to wrap it into an async function because Deno implements top-level await.

Now, let’s run it. But wait, we know Deno is secure by default, thus we will need to allow the permission to use the internet. This is how we will run this file,

deno run --allow-net app.ts

Here, --allow-net is the flag that will give permission to access the internet.

Pretty simple, right? Deno seems amazing!
Well, that’s it for this article. Hope it helped you to get started with Deno. We don’t know what the future holds for Deno, but, it’s quite young and seems promising.

Print Statement - Hello World

Want to learn programming and web development? Try Programming Hero, a fun way to learn to code.

Related Post

related post

If you spend 3–4 hours daily for 7 days, you will become a Junior Python Developer. I am providing this guideline using this android app called: Programming Hero.

related post

I am taking all the examples from the Python learning app called Programming Hero: Coding Just Got Fun