Skip to content

What’s a framework anyway?

A framework is a collection of tools that provide common functionality for a certain type of application. Frameworks allow developers to quickly start working on business outcomes. We can start solving our problems instead of those that have been solved already.

It can be difficult to know what framework is the best fit for your needs. As software developers, we’re often asked to make technology choices with limited information.

Web frameworks download trends (NPM trends)


Express

Express is older and more established. Express code looks and feels more like native Node.

Express uses middleware to provide developers access to the request/response pipeline. Developers have access to Node’s req and res request/response objects. An Express application “chains” middleware together to act on requests and responses. Each middleware component has a single, well-defined job to do, keeping concerns isolated within each component.

Express.js is described as the standard server framework for Node.js. It was created by TJ Holowaychuk, acquired by StrongLoop in 2014, and is currently maintained by the Node.js Foundation incubator. With about 170+ million downloads in the last year, it’s currently beyond doubt that it’s the most popular Node.js framework.

Hello world

var express = require('express');
var cookieParser = require('cookie-parser');

var app = express();
app.use(cookieParser());

app.get('/', function (req, res) {
  console.log("Cookies: ", req.cookies);
  res.cookie("greeted", "true").send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Read more: https://expressjs.com/


Hapi

Hapi (short for (Http)API, pronounced “happy”) is a newer framework that abstracts the existing Node API.

Hapi uses plugins to extend its capabilities. Plugins are configured at runtime through code. There is usually a Hapi plugin for every Express middleware component, making Express and Hapi more or less equal regarding capabilities.

Hapi was developed at Walmart to provide a rock solid foundation for their e-commerce business. Now spun out as open source framework with no ties to its originating company, Hapi remains popular for larger.

Hapi comes with a large set of separate, but tightly integrated and supported plugins for features like logging, templating, caching, error handling etc. It’s object validation plugin Joi is almost as popular as the framework itself.

In the community, Hapi is praised for its API, its robustness and reliability. Also, Hapi does not have any external code dependencies making security issues much more controllable.

Hello world

'use strict';

const Hapi = require('hapi');

// Create a server with a host and port  
const server = Hapi.server({
  host: 'localhost',
  port: 8000
});

// Add the route  
server.route({
  method: 'GET',
  path: '/hello',
  handler: function (request, h) {

    return 'Hello World!';
  }
});

// Start the server  
async function start () {

  try {
    await server.start();
  }
  catch (err) {
    console.log(err);
    process.exit(1);
  }

  console.log('Server running at:', server.info.uri);
};

start();

Read more: https://hapi.dev/


Restify

Restify is dedicated to RestAPI and RestAPI only.

Restify is squarely aimed at providing a framework for building RESTful APIs, where other frameworks tend to solve API, static content and template parsing problems. It is used in production by NPM, Netflix, Joyent and Pinterest.

This focus translates into its documentation and guides. They are simple and to the point. Extensions are build using the plugin API, although the third party plugins are spread very thin.

Stressing debuggability as one of its main pilllars, it is cool to see that Restify includes automatic generation of Dtrace probes, a feature not commonly found in any of the other frameworks.

Hello world

var restify = require('restify'), // require the restify library.
  server = restify.createServer(); // create an HTTP server.

// add a route that listens on http://localhost:5000/hello/world
server.get('/hello', function (req, res, cb) {
  res.send("Hello World!");
  return cb();
});

server.listen(process.env.PORT || 5000, function () { // bind server to port 5000.
  console.log('%s listening at %s', server.name, server.url);
});

Read more: http://restify.com/


Fastify

Fastify is an API framework 100% aimed at performance. Claiming to be inspired by Hapi and Express, the maintainers have chosen to focus on balancing developer experience with raw speed and performance.

Although Fastify is again a very minimal framework, the middleware architecture is compatible with Express and Restify middlewares, greatly expanding possible use cases.

Fastify comes with experimental HTTP 2.0 support and ships with TypeScript typings.

Hello world

const fastify = require('fastify')()

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()

Read more: https://www.fastify.io/


Further reading