NodeJS vs. Deno - A Simple Web Server (2021)

NodeJS vs. Deno - A Simple Web Server (2021)

I created a simple web server with NodeJS and Deno

Pascal Lamers's photo
Pascal Lamers

Published on Oct 1, 2021

3 min read

Some time ago, Deno was released into the wild. As a NodeJS enthusiast that I am it didn't took long before I read about it on several tech blogs and Twitter.

But I naturally lost interest again, since at the time I didn't encounter any problems with NodeJS, which Deno could have helped me with. Also it was still early so I decided to let it grow before checking it out.

So, fast forward one Corona Virus Epidemic, I am sitting in front of my perfectly fine NodeJS-Express-Server, wondering what would it take to re-create the exact same thing, not in NodeJS but in Deno ? 🤔 So let's npm start ...

Oh btw, I won't go into detail about how to setup the one or the other, I'll jump directly into coding cause that's way more fun.

NodeJS

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.status(200).send('ok'); 
}); 

app.listen(8080, () => {
  console.log('hello world on localhost:8080');
});

Nothing too fancy going on here. I am using a third-party dependency called express, which I notably need to install before I can use it, using npm install express in my project. Then I register a GET route endpoint on the / path and finally start listening on the 8080 port. I start the app with node file.js

Deno

import { opine } from "https://deno.land/x/opine@1.8.0/mod.ts";
const app = opine();

app.get('/', function (req, res) {
  res.setStatus(200).send('ok');
});

app.listen(8080, () => {
  console.log('hello world on localhost:8080')
});

Putting aside the fact that this example is like super simple, the only thing noticeable different here using Deno is the way I get the third party dependency in. I am importing opine (an ExpressJS equivalent for Deno) directly from an URL. That means I don't have to install this dependency beforehand. When I first start the app it downloads all needed deps so it takes a bit longer at first.

Other noticable differences

  • At least in this quick example there is not much difference from the code side of things but it would only be fair to note that Deno comes with a built-in TypeScript compiler. That means I could use both, Javascript and TypeScript, out of the box. As with NodeJS there would be additional setup steps to support TypeScript. However I went with plain Javascript on both examples.

  • To run the app with NodeJS I just do node file.js and start using it. With Deno however I needed to use deno run --allow-read=%cd% --allow-net file.ts. That's because Deno comes with built-in security features. By default it is not allowed to breathe unless you tell it otherwise - like reading from file system or listening to a port.

  • Maybe it's just me but I noticed that Deno, even if the deps already are downloaded, still takes longer to compile and start the app than NodeJS.

Conclusion

For me and for now the only reason I would instantly switch to Deno would be if I want to create an app using TypeScript. I haven't actively used TypeScript in a project before so ... having it right there from the start is a big plus for me when using Deno. Until that's the case I probably stick with NodeJS.

Thanks for reading ❤

 
Share this