19
Get started with deno (from NodeJS or not...)
If you have been working with NodeJS or not even, you surely heard about Deno (no-De) and that's no hazard. Javascript, Typescript and NodeJS are technologies that have been on the rise for quite a while now. Well what if I told you that those three technologies are actually implemented by default in Deno, without you having to do much to set them up if not any.
Just like NodeJS, Deno is a runtime for Javascript. And also like NodeJS it is Authored by the same person that worked on NodeJS, mister Ryan Dahl. This project is actually not that recent. It was first announced in 2018 during a talk "10 things I regret about NodeJS". It's important to take this lightly as NodeJS is still and will continue to be a stable and robust environment to code in. But some initial design decisions have ignited a will to change or enhance the existent. Deno was first written in Go but was then rewrote in Rust, another rising technology that I would definitely recommend you learn about. Deno's official 1.0 version was released on May 13, 2020 and the current lts version is 1.10.0.
The most important change, and difference that deno brings compared to NodeJS is security.
Deno's core was written in Rust and Tokio which is the asynchronous run time for Rust. Rust itself is a language that is focused on performance and safety most importantly when it comes to concurrency. It provides a safer way to manage memory without having to use a garbage collector. On top of that, deno does not allow any type of file, environment or network access unless you actually give it the rights. Even so, those permissions are scoped and do not provide a wide range of access.
Across different projects, if we want to have these 3 principles in place with NodeJS, a lot of configuration, installation and tweaking are required. This pollutes the workspace and complicates deployments.
Deno ships Typescript, a linter and an assertion library out of the box without needing any configuration whatsoever. You can still configure if you wish but that will probably not be needed.
In any case, Deno ships a lot more things but this is enough to get us started. Any additional information can be found on the official repository url that I will be linking at the end of the article.
As for a little start, since nodejs has been used as a very popular web server, I thought it might be interesting to start building a deno version of it.
To start off, let's install deno. Depending on your OS you might want to refer to Install Docs
curl -fsSL https://deno.land/x/install/install.sh | sh -s v0.38.0
That's basically it! We don't need to look at typescript configurations we can directly start up creating our first deno file.
touch api.ts
Now just like nodejs, Deno ships a ready to use http server package that we need to import.
import { serve } from "https://deno.land/[email protected]/http/server.ts";
Next, let's go ahead and import our sql. Of course, I am using sqlite for demo purposes but you might go ahead and get whatever suites your needs.
import { DB } from "<https://deno.land/x/sqlite/mod.ts>";
Finally, let's add some simple sample of code to run our http server
const PORT = 8162;
const s = serve({ port: PORT });
console.log(` Listening on <http://localhost>:${PORT}/`);
for await (const req of s) {
req.respond({ body: "Hello World\\n" });
}
And voila! A running http server. Now compared to nodejs, what was not needed to do beforehand:
- Install manually the sqlite dependency
- Install manually typescript, and configure Now, we did say a running http server but that may not mean it is fully functional. Remember we said authorizations are required in order for deno to access different functionalities? Well http or network access requires a specific authorization. In order to do this we must append a flag to our deno command as such:
Deno api —allow-net
We can now go ahead and on our localhost:8162 to check out our newly created HTTP Deno server!
Now on to using that sqlite module we imported. It wouldn't be a >simple< get started tutorial with some basic database action so here is a snippet of code to get started :
// Open a database
const db = new DB("test.db");
db.query(`
CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
)
`);
const names = ["Peter Parker", "Clark Kent", "Bruce Wayne"];
// Run a simple query
for (const name of names) {
db.query("INSERT INTO people (name) VALUES (?)", [name]);
}
// Print out data in table
for (const [name] of db.query("SELECT name FROM people")) {
console.log(name);
}
// Close connection
db.close();
Now we can open a database, run a query and print out data. If you weren't in a Deno article, you would have probably never realized that this is not nodejs and that is the whole point! Deno is not, a fundamental coding change coming from nodejs. And if you start with Deno, you could totally switch to nodejs without loosing any of the acquired knowledge.
Now as you can see if you come from nodejs or from a JavaScript environment, there isn't much difference from what you know. Although, if you actually come from nodejs, you would find interesting that the root nodejs api is fully promised built. The difference and additions are more at the root of the software. As a growing technology, nodejs needed a security upgrade. Again as a growing technology, nodejs also needed a simple environment that is not bloated in dozens of config files. As of me, I would use Deno for from scratch projects. I don't feel like I am getting onboard of a totally new ship. Documentation is understandable and modules are maintained if they are featured on the official deno.land page. Now should you upgrade your existing projects from nodejs to deno? Deno does give you some upgrading best practices and guidelines but it is not something that will get done with a snap of the fingers. Keep in mind, deno is not here to replace node but to give a more secure and less error-prone environment. Node is still going to be out there and will probably never get less popular than Deno. But Deno is a good alternative if you are looking for a strict, secure and tidy Javascript runtime environment to code your favorite projects on!
19