Overview of Deno JS - Installation and Building an API

Featured on Hashnode

Subscribe to my newsletter and never miss my upcoming articles

Overview of DENO JS

Deno is a JavaScript/TypeScript runtime created by Ryan Dahl the same developer who created Node js It uses URLs for loading local or remote dependencies, similar to browsers. And it includes a built-in package manager for resource fetching, thus no need for NPM.

Advantages of using Deno JS

  • Built in TypeScript Support
  • More Secure
  • ES6+ Syntax
  • No package.json or Node modules

Installation

For MAC, simply execute this command

brew install deno

For Windows, execute this command (Hope chocolatey is already installed)

choco install deno

Building an API

We will build a API to operate crud operations required for our TODO Application

And these are End points

GET - /todos Used to read all the data
POST - /todos (form data) Used to insert the data
PUT - /todos/:id Used to update an existing data
DELETE - /todos/:id Used to delete an record
Step 1:

Well, we will start with index.js Normally, in NodeJs we use express for routings and we import it using require keyword but in DENO Js we import from oak (As you can see in below code)

import { Application } from "https://deno.land/x/oak/mod.ts";
import { PORT } from "./config.js";
import router from "./router.js";

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

console.log(`Server is running. Open http://localhost:${PORT}`);

await app.listen({ port: PORT });

Step 2:

Setup configuration file i.e PORT and FilePath required to store JSON data

export const PORT = 5000;
export const FilePath = "./data/todos.json";

Step 3:

Now create get.js, post.js, delete.js, put.js and place it in the controller. For clear code view, you can check my GitHub repository.

Step 4:

Now create a router.js file which handles all the routings that are required for the application

import { Router } from "https://deno.land/x/oak/mod.ts";
import getTodos from "./Controllers/todos/get.js";
import postTodo from "./Controllers/todos/post.js";
import deleteTodo from "./Controllers/todos/delete.js";
import putTodo from "./Controllers/todos/put.js";

const router = new Router();

router.get("/", ({ response }) => {
  response.body = "Todo Application using Deno";
});

router
  .get("/todos", getTodos)
  .post("/todos", postTodo)
  .delete("/todos/:id", deleteTodo)
  .put("/todos/:id", putTodo);

export default router;

Step 5:

The main difference in Deno to NodeJs is the flags which need to be passed while executing the application for read, write permissions And this is one of the reason Deno make more secure.

 deno run --allow-net=:5000 --allow-read=./data/todos.json --allow-write=./data/todos.json ./index.js

Deno loads the modules once from the URL and cache it in your system. To reload the modules again, you need to provide --reload flag before running the program.

deno run --reload --allow-net ./index.js

Step 6:

Now we can test our application in postman

Get

Alt Text

Post

Alt Text

Delete

Alt Text

Put

Alt Text

Conclusion:

Well, Deno is still in early stages and it still needs to be crafted. So the answer for Whether Deno kills Node ? is totally unclear.

Hope it's useful A ❀️ would-be Awesome 😊

EsaΓΊ Morais's photo

Simple and useful, thank you 🀩

Note to Ubuntu users: he didn't say how works Deno installation. So, here is:

  • Add deno using CURL: curl -fsSL https://deno.land/x/install/install.sh | sh

  • Use nano to edit the file and add these two lines: sudo nano .bashrc

export DENO_INSTALL="/$HOME/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
  • Execute the file: source ~/.bashrc

  • That's it, run: deno

Sunil Aleti's photo

Thanks for mentioning :)

Edidiong Asikpo's photo

Very insightful read. Thanks for sharing Sunil Aleti.