What is Deno?
A secure runtime for JavaScript and TypeScript.
Based on their website:
Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.
- Secure by default. No file, network, or environment access, unless explicitly enabled.
- Supports TypeScript out of the box.
- Ships only a single executable file.
- Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
- Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno: deno.land/std
These were from Deno's official document.
Why Deno is created and who is the creator?
Deno is created by Ryan Dahl and he is the creator of NodeJS. He mentioned 10 things he regrets about NodeJS and he tried to solve those issues in Deno. This is the Video 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.
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
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 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. 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.
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 discussed the web frameworks and it contains useful tables like this:
Implement one example using one of Deno’s web framework
I chose Opine since it is really similar to 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
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.
Top comments (0)