loading...
Cover image for Overview of Deno JS - Installation and Building an API

Overview of Deno JS - Installation and Building an API

sunilaleti profile image Sunil Aleti ・3 min read

GitHub logo aletisunil / TODO-Application-using-DENO

Creating an API using DENO for a TODO Application

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 😊

Happy_Coding

Posted on by:

sunilaleti profile

Sunil Aleti

@sunilaleti

Passionate Data Scientist | Blogger | Python Developer

Discussion

pic
Editor guide
 

That's a great intro to Deno, Sunil!

One thing to keep in mind when it comes to Deno's permissions, is that the following is not ideal:

deno run --allow-net --allow-read --allow-write ./index.js

This allows the Deno application to send requests to any URL, read any file and write any file. In your case, what you want to be secure is the following:

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

I opened a PR for your repo as well.

 

Hey Mike,
Yes whatever you are saying is true, the main aim of DENO is make more secure. I seriously forgot about setting values to the flags.
Kudos to you and Thanks a lot man

 

Thank you. Concise.

 

Thanks 😊

 

Deno is some really exciting tech to keep an eye on right now, or become an early adopter!

 

yes, And it will totally depend on future Deno rollouts too..

 

Thank you. Great introduction to Deno runtime

 

Yould could have have done domain separation. App/Todo/Cntroller.js and add methods for GET, POST, DELETE and PUT.

Why no PATCH?

Also I think you missed an opportunity to do it in TS.