DENO : Launch, Install, Features and REST APIs (CRUD)


Deno is coming

Deno, A better NodeJs or a secure runtime environment with some new ways of re-writing our node applications. In this article, we will discuss and compare DENO with NODEJS, its launch date, installation, features, permissions, http-server, REST APIs, and will it be successful.


DENO is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on security and productivity.

It has already 49k stars on the Github.


As everyone knows, DENO is launching on 13 May 2020. So you must be thinking is there something that you need. Of course, As new technologies and programming languages are surrounding programmers we must be aware what is coming and how it can help or make our life simple in terms of programming new things. So yes, we need to take a look at DENO.


You can say it is the next version of Nodejs but with all new paradigms and new secure runtime which improves our productivity in an efficient way. Let's jump to see how it can do this.


Installation

Using Shell:

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

Or using PowerShell:

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


Modules

Deno, just like browsers, loads modules by URLs. Many people got confused at first when they saw an import statement with a URL on the server-side, but it actually makes sense — just bear with me:


import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

What’s the big deal with importing packages by their URLs, you may ask? The answer is simple: by using URLs, Deno packages can be distributed without a centralized registry such as npm, which recently has had a lot of problems, all of them explained here.

By importing code via URL, we make it possible for package creators to host their code wherever they see fit — decentralization at its finest. No more package.json and node_modules.

When we start the application, Deno downloads all the imported modules and caches them. Once they are cached, Deno will not download them again until we specifically ask for it with the --reload flag.



Security (permissions)

Among the most important of Deno’s features is its focus on security.

As opposed to Node, Deno by default executes the code in a sandbox, which means that runtime has no access to:

  • The file system

  • The network

  • Execution of other scripts

  • The environment variables

Let’s take a look at how the permission system works.






As you can see in the above images, how at first Deno environment didn't allow us to do our task but after passing the flags, it will know that we are reading or writing any file or hitting outer-network willingly and then it will allow us.

The --allow-write, --allow-read flag for the file system, there are also --allow-net, --allow-env, and --allow-run flags to enable network requests, access the environment, and for running subprocesses, respectively.


TypeScript support out of the box


Deno makes it easy to use TypeScript without the need for any config files. Still, it is possible to write programs in plain JavaScript and execute them with Deno without any trouble.


Compile-Time Check


With Deno, we can also check our coding error in compile time like Java, C, python. In our example, in myName function, I want the name parameter of string type. But when I am calling it I passed a numerical value in it which gives me an error when I tried to run my file.



Bundle


Deno lets you bundle all your ts files in one file of whatever you name it.


VS-Code Extension



(Http-Server) REST APIs implementing Create, Read, Update, Delete (CRUD)


To implement Http-server in Deno, I am using OAK server (A middleware framework for Deno's net server🦕).


In the below example I will use users.json file to read and write the data of users using REST APIs.

Users.json

{
 "user1":{
        "name":"Ryan",
        "password":"password1",
        "profession":"teacher",
        "id":1
    },
 "user2":{
        "name":"Medwid",
        "password":"password2",
        "profession":"clerk",
        "id":2
    },
 "user3":{
        "name":"George",
        "password":"password3",
        "profession":"webdeveloper",
        "id":3
    }
}

index.ts


import { Application, Router } from "https://deno.land/x/oak/mod.ts";


const router = new Router();
router.get("/", (context : any) => {
 context.response.body = "Hello world!";
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
console.log("Server is running on address : http://localhost:8000")
await app.listen({ port: 8000 })

To run the code

deno run --allow-net --allow-read --allow-write index.ts


Now we will write our HTTP methods to read, create, delete, and update the users in users.json file


/getUser API


router.get("/getUser",(context : any) => {
 try{
     const decoder = new TextDecoder("utf-8");
     const data = Deno.readFileSync("./users.json");
     console.log(decoder.decode(data));
     context.response.body = decoder.decode(data)
    }
 catch(err){
     console.log(err);
     context.response.status = 500;
     context.response.body = err;
    }
 
})  

1. context : This object have our both request and response methods which we can use to send status, headers, and response body.

2. TextDecoder : This class will decode the file contents in utf-8 standards.



Full Code







Lets see how its launch goes and how it will perform in the coming timeframe.

30 views

Subscribe to Our Newsletter

© 2023 by BLIO

  • YouTube
  • Instagram
  • reddit-logo-newnew
  • medium-logo
  • LinkedIn
  • github-logo-new
  • Twitter
  • tiktok-logo-newnew
  • Facebook