alternativas-a-expressjs-en-nodejs

Alternatives to Express.js in Node.js

  • 3 min

When it comes to creating web servers and applications in Node.js, express.js has long been the undisputed king, to the point of almost becoming a standard.

However, it’s a library that is now several years old. Consequently, many interesting alternatives have emerged that are worth at least knowing about.

This does not mean we have to stop using express.js. In fact, it works perfectly. It’s simply interesting to stay up-to-date with some of the available options.

Koa

koa

Koa, created by the same team behind Express, is a lightweight and modular framework that uses function generators for a cleaner and easier-to-understand workflow.

Some of its features include:

  • Focus on modularity and ease of use
  • Out-of-the-box support for async/await
  • Clearer and less cumbersome middleware abstraction

Basic Koa example

import Koa from 'koa';
const app = new Koa();

app.use(async (ctx) => {
  ctx.body = 'Hello, Koa!';
});

app.listen(3000);
Copied!

Hapi

hapi

Hapi is a robust and highly configurable web framework, ideal for large and complex applications.

Some notable features are:

  • Strong emphasis on configuration and modularity
  • Built-in tools for creating RESTful APIs
  • Facilities for data and schema validation

Basic Hapi example,

import Hapi from '@hapi/hapi';

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  server.route({
    method: 'GET',
    path: '/',
    handler: (request, h) => {
      return 'Hello, Hapi!';
    }
  });

  await server.start();
  console.log('Server started at:', server.info.uri);
};

init();
Copied!

Fastify

fastify

Fastify stands out for its focus on performance and efficiency. It is one of the fastest web frameworks available in Node.js and offers:

  • Low overhead and latency
  • Support for handling HTTP requests efficiently
  • A plugin system that allows easy extension of its functionality

Basic Fastify example,

import fastify from 'fastify';

const app = fastify({ logger: true });

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

app.listen(3000, (err, address) => {
  if (err) throw err;
  console.log(`Server started at: ${address}`);
});
Copied!

NestJS

nestjs

NestJS is a framework that uses TypeScript and is inspired by Angular. Although it is more oriented towards complete web applications, it offers:

  • Architecture based on modules and decorators
  • Easy integration with external libraries
  • Support for creating scalable and maintainable applications

Basic NestJS example,

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
Copied!

AdonisJS

adonisjs

AdonisJS is a complete MVC framework inspired by Laravel (from PHP).

Some of its features are:

  • Complete project structure and automatic code generator
  • Ease of use and rapid development
  • Built-in database support and session management

Basic AdonisJS example,

// server.js
import { Ignitor } from '@adonisjs/ignitor';

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .fireHttpServer()
  .catch(console.error);
Copied!