# Overview of Deno JS - Installation and Building an API

<h3><b>Overview of DENO JS</b></h3>
Deno is a JavaScript/TypeScript runtime created by <b>Ryan Dahl</b> the same developer who created <b>Node js</b>
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.


<h3><b>Advantages of using Deno JS</b></h3>
<ul>
<li>Built in TypeScript Support</li>
<li>More Secure</li>
<li>ES6+ Syntax</li>
<li>No package.json or Node modules</li>
</ul>


<h3><b>Installation</b></h3>
For MAC, simply execute this command
``` javascript
brew install deno
 ```
For Windows, execute this command (Hope chocolatey is already installed)
``` javascript
choco install deno
``` 

<h3><b>Building an API</b></h3> 
We will build a API to operate crud operations required for our TODO Application

And these are End points
<table>
<tr><td><b>GET</b> - /todos</td>  <td>Used to read all the data</td></tr>
<tr><td><b>POST</b> - /todos (form data)</td><td>  Used to insert the data</td></tr>
<tr><td><b>PUT</b> - /todos/:id</td><td> Used to update an existing data</td></tr>
<tr><td><b>DELETE</b> - /todos/:id</td><td> Used to delete an record</td></tr>
</table>
<b>Step 1:</b> 

Well, we will start with index.js
Normally, in NodeJs we use express for routings and we import it using <b>require</b> keyword but in DENO Js we import from <a src="https://deno.land/x/oak/mod.ts">oak</a> (As you can see in below code)
```javascript
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 });
```

<b>Step 2:</b>

Setup configuration file i.e PORT and FilePath required to store JSON data
``` javascript
export const PORT = 5000;
export const FilePath = "./data/todos.json";
```
<b>Step 3:</b>

Now create get.js, post.js, delete.js, put.js and place it in the controller.
For clear code view, you can check my <a href="https://github.com/aletisunil/TODO-Application-using-DENO">GitHub repository.</a>

<b>Step 4:</b> 

Now create a router.js file which handles all the routings that are required for the application
``` javascript
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;
```

<b>Step 5:</b>

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.
``` javascript
 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.
``` javascript
deno run --reload --allow-net ./index.js
```

<b>Step 6:</b>

Now we can test our application in postman

<h4>Get</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mnkomyq81bx3of0ng3gf.png)

<h4>Post</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6mz623qlctsv7353etqg.png)

<h4>Delete</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7pipa2rvk8c5g4k22guf.png)

<h4>Put</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yrhvz0136grg1q3777i4.png)

<h3><b>Conclusion:</b></h3>
Well, Deno is still in early stages and it still needs to be crafted.
So the answer for <b>Whether Deno kills Node ?</b> is totally unclear.

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

%%[buymeacoffee]

