First Time Using Deno
Tags: webdev, deno, javascript, node
Published: 2023-09-12 11:59:39

---
title: First Time Using Deno
published: true
date: 2020-10-26 19:25:49 UTC
tags: webdevelopment,deno,javascript,nodejs
canonical_url: https://medium.com/weekly-webtips/first-time-using-deno-b0538dd4fc47
---

![](https://cdn-images-1.medium.com/max/1024/1*aHszCFF-U1pQrWl2t04KhA.png)
_First time using Deno_

#### What is Deno?

A secure runtime for JavaScript and TypeScript.  
_Based on their_ [_website_](https://deno.land/)_:_  
Deno is a _simple_, _modern,_ and _secure runtime_ for JavaScript and TypeScript that uses V8 and is built in _Rust_.

1. Secure by default. No file, network, or environment access, unless explicitly enabled.
2. Supports TypeScript out of the box.
3. Ships only a single executable file.
4. Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
5. Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno: [deno.land/std](https://deno.land/std)

[These were from Deno's official document](http://deno.land).

#### Why Deno is created and who is the creator?

Deno is created by [Ryan Dahl](https://en.wikipedia.org/wiki/Ryan_Dahl) and he is the creator of [NodeJS](https://nodejs.org/en/). He mentioned 10 things he regrets about NodeJS and he tried to solve those issues in Deno. This is the [Video](https://www.youtube.com/watch?v=M3BM9TB-8yA&t=601s&ab_channel=JSConf) of the conference and I strongly recommend seeing this video cause it could bring a few questions into your mind. For example, why should we have a node\_module or package.json?

#### Is it a good idea to switch to Deno from Node?

In my opinion, it is a bit soon to switch to Deno since its community is not big enough and it doesn’t win the performance battle. I believe that Deno could win this battle in the future.

#### Deno Installation

You can install it using package managers or download it directly from [here](https://github.com/denoland/deno/releases).

If you are using Homebrew on Mac or Linux you can install Deno using this command:

```
brew install deno
```

#### Simple Http server

Use this code in a typescript file:

```
import { serve } from "https://deno.land/std@0.74.0/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" });

}
```

And now you can run it using this command:

```
deno run --allow-net server.ts 
```

The reason we used --allow-net is that in Deno we have to strictly tell it that we want to use the network.

If you are using **visual studio code** like me you might encounter this problem:

![Deno warnings on visual studio code](https://cdn-images-1.medium.com/max/1024/1*5hwUNhq2FZ5rtwY8YXCPeQ.png)
_Deno warnings on visual studio code_

The typescript compiler has a problem with defining the extension which is something that Ryan Dahl believed that it is not a good idea to remove the extension. There are a few Github issues like [this](https://github.com/Microsoft/TypeScript/issues/27481) one but the typescript team didn’t have a solution for this issue and it might be logical since this is the way NodeJS resolves the modules. In order to solve this, we can use an extension named [vscode-deno](https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno). After that, you need to enable Deno on your settings.json . You can open the settings.json by using _command+shift+P_ and typing setting. After opening the config file add this key value to the JSON file: "deno.enable":true . This will solve the problem in vscode.

#### Is there anything like Nodemon to reload my server with each file change?

Yes, and its name is **_Denon_**.   
Installation:

```
deno install --allow-read --allow-run --allow-write -f --unstable https://deno.land/x/denon/denon.ts
```

Running your app is similar to Deno: denon --allow-net app.ts but you need to add the Denon to your path. On Mac it is in this folder: /Users/pshaddel/.deno/bin/denon .

To run our server again we can use this command:

```
denon --allow-net server.ts
```

If you want more information about Denon, take a look at [this article](https://medium.com/recoding/creating-hot-reload-or-live-reload-in-deno-app-using-denom-a62c58e0b878).

**What are web frameworks and libraries to implement an HTTP server?**

There are a few web frameworks available on Deno: opine, oak, abc, denotrain, …  
[This article](https://dev.to/craigmorten/what-is-the-best-deno-web-framework-2k69) discussed the web frameworks and it contains useful tables like this:

![Compare Deno web frameworks](https://cdn-images-1.medium.com/max/1024/1*Jspi0fWxbadTcnNezX-j9A.png)
_Compare Deno web frameworks_

#### Implement one example using one of Deno’s web framework

I chose [Opine](https://github.com/asos-craigmorten/opine) since it is really similar to [Express](https://github.com/expressjs/express).

```
import { opine } from "https://deno.land/x/opine@0.25.0/mod.ts";

const app = opine();

app.get("/", (req, res) => {

res.json({ page: "home" });

});

app.post(`/users`, (req, res) => {

res.send(`you sent

POST request to /users`);

});

app.listen(8000);
```

If the opine package is not on your local machine then the code editor(vscode) gives you a quick fix and fetches the package for you.

app instance is like an express application and we can call methods like use , get and post on it.

To run the file, use this command:

```
denon --allow-net --allow-read server.ts
```

Why we need --allow-read opine needs this to access the public folder and static files(which we didn’t have). The result of the requests is shown below:

![Sending request to our Opine app](https://cdn-images-1.medium.com/max/1024/1*RBZw8eXk0CoTnN4EpgqQfw.png)
_Sending request to our Opine app_

#### Conclusion

Deno solved some issues of NodeJS and it has a bright future in my opinion. The web framework I used(Opine) was really similar to Express and I didn’t need any extra information to do it.

* * *