Fastify, Fast and low overhead web framework, for Node.js

It’s been a while since I’ve set up a server with Node, but turns out Fastify is preferred over Express nowadays.

Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

import Fastify from 'fastify';
const fastify = Fastify({ logger: true });

fastify.get('/', async (request, reply) => {
  return { hello: 'world' };
});

const start = async () => {
  try {
    await fastify.listen(3000);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
}
start();

Apart from the core there are lots of plugins for authentication, cors, forms, cookies, jwt tokens, etc.

Fastify, Fast and low overhead web framework, for Node.js →

CommonJS to ESM in Node.js

With Node 12 and up supporting ES Modules natively and Node 10 — the last version to not support it unflagged — going EOL, it’s time to start migrating your code to ES Modules. Aral Balkan took the time to convert his tool “Place” to embrace them and documented his work:

Yesterday, I refactored Place, which is a non-trivial Node.js app, to use ECMAScript Modules (ESM). Here’s a diff of the full set of changes.

This is the approach I took and some of the issues I ran into, in case it helps someone else

First step: set "type": "module" in your package.json

CommonJS to ESM in Node.js →

10 best practices to containerize Node.js web applications with Docker

Solid list of tips by the folks over at Snyk:

By the time you’re at number 8 your mind may have dwelled, but don’t skip out on that step though! It not only allows you to build smaller images but also prevents you from having unnecessary files (read: security risks) left inside your container. Basically you install your dependencies in one throwaway container, and then copy the resulting folder into your main container.

10 best practices to containerize Node.js web applications with Docker →

Running the same Node.js code on Google Cloud Functions, App Engine, and Cloud Run

Google Cloud has a number of options to run your code. We can deploy a function to Cloud Functions, an app to App Engine, or an app with a custom runtime (a Docker container) to Cloud Run.

In this post the same code snippet is deployed to all three Google Cloud Platform features. Locally the Functions Framework is used .

Portable code migrating across Google Cloud’s serverless platforms →

Optimizing JavaScript packages for tree shaking

Geoffrey Dhuyvetters from madewithlove on how authors of (open source) JavaScript packages can optimize their builds for tree shaking:

How do we create a package that exposes both CommonJS & ES modules while making sure we don’t break cross-platform support? Publishing 2 separate packages is an option (e.g. lodash/lodash-es). But there is a nicer, more maintainable option that obviates the need to publish twice. We can provide an extra build step that creates an ES version of our package and links it via package.json.

The package.json links both builds like so:

{
  …
  "main": "build/cjs/index.js",
  "module": "build/esm/index.js",
  …
}

Didn’t know you could als provide a module field, similar to name … handy!

Optimizing JavaScript packages for tree shaking →

Deno, a new way to JavaScript

Ryan Dahl – inventor of Node.js – has been working on Deno, a new take on Node as if it were designed today.

From async-await to ArrayBuffers, the JavaScript language has changed significantly in the decade since Node.js was designed. Deno takes advantage of these developments and incorporate lessons learned in the development of Node to provide a new JavaScript platform built on V8 and Rust. This talk will teach the audience how to get started with Deno.

I especially like the compatibility with ES Modules.

Slides are also available.

🎧 FYI: The audio gets better after a few minutes. No worries there 😉