What’s new with Deno 1.14

I’m personally interesting of the evolution of Deno. Specially, after the announcement in JSConf 2018 by Ryan Dahl “10 thinks I regret About Node.js”, I figure it out that node js will die in five next years.

As a fullstack web developer, I tried to create APIs using Deno. But, I was stuck because the lack of necessary packages. I really like making fast web servers using different programming language like Go, Rust, Python and Ruby. But, for deno, I may be wrong at this point but, companies I worked with still don’t know it 🤨 And personally, I see that it’s not that stable to be confident with it unlike Node JS and Python.

The, last stable version of Deno was released. So, let’s see what changes may have been released.

1 Request / Response classes

These classes are the basics of the web API. Request give us the body object, when we POST to an API, Query and params that we pass in URL (GET). In a web application, it’s a must that encoding and mapping data are easy to extract and save them in database/file. Mapping these object is not that easy when we code from scratch, specially when we have to POST a file.

2 Crypto with Deno

Security is very important by hashing a password before saving or comparing a checksum file. It’s necessary for encrypting, decrypting and verifying data, specially for TLS/SSL concepts. The deno core team said that it’ll be complete API by the end of the year.

3 Database drivers

When we deal with database, we use drivers, unsupported drivers means bugs to connect with database and crash nightmares. Popular database like MariaDB and PostgreSQL have already supported drivers written in many languages. This Repository contains supported drivers for deno https://github.com/denodrivers

3 HTTP/HTTPS Client

Some APIs have to connect with other APIs, by sending HTTP/S requests. Using Promises helps us dealing with differents errors. Imagine calling three services to save data and the user endpoints returns a 403 Forbidden error. These common scenarii are repetitives and annoying specially when we have to authenticate.

4 WebSocket in Deno

WebSocket is the most important part for creating interactive web application. Today, many websites include websockets to provide a full duplex communication over TCP.

The last Deno release supports HTTP requests, but the native HTTP server still unstable and need more supports.

This is an example to how to create a websocket server with Deno:

// https://deno.com/v1.12/ws_server.js

async function handleConn(conn) {
  const httpConn = Deno.serveHttp(conn);
  for await (const e of httpConn) {
    e.respondWith(handle(e.request));
  }
}

function handle(req) {
  if (req.headers.get("upgrade") != "websocket") {
    return new Response("not trying to upgrade as websocket.");
  }
  const { websocket, response } = Deno.upgradeWebSocket(req);
  websocket.onopen = () => console.log("socket opened");
  websocket.onmessage = (e) => {
    console.log("socket message:", e.data);
    websocket.send(new Date().toString());
  };
  websocket.onerror = (e) => console.log("socket errored:", e.message);
  websocket.onclose = () => console.log("socket closed");
  return response;
}

const listener = Deno.listen({ port: 8080 });
console.log("listening on http://localhost:8080");
for await (const conn of listener) {
  handleConn(conn);
}

To test this script, fist, we have to run it with: deno run --allow-net --unstable https://deno.com/v1.12/ws_server.js. And then run this is chrome console:

> const ws = new WebSocket("ws://localhost:8080/");
  ws.onmessage = (e) => console.log(e.data);
> ws.send("hi");

Conclusion

Actually, as a senior web developer, I can’t force my clients to chose Deno to make RESTful APIs. But, maybe, the node communities will switch all necessary packages to Deno and then, who knows? May be it’ll replace Node JS.

For mor details

You Might Also Like

Leave a Reply