Language: EN

alternativas-a-expressjs-en-nodejs

Alternatives to Express.js in Node.js

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

However, it is a library that has been around for many years. Therefore, many interesting alternatives have emerged that are worth at least knowing about.

This does not mean that we have to stop using express.js. In fact, it works perfectly. It is just 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.
  • Support for async/await out of the box.
  • Clearer and less cumbersome middleware abstraction.

Basic example of Koa

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

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

app.listen(3000);

Hapi

hapi

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

Some highlighted features are:

  • Great emphasis on configuration and modularity.
  • Integrated tools for creating RESTful APIs.
  • Facilities for data validation and schemas.

Basic example of Hapi,

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

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 example of Fastify,

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}`);
});

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:

  • Module-based architecture and decorators.
  • Easy integration with external libraries.
  • Support for creating scalable and maintainable applications.

Basic example of NestJS,

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

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.
  • Support for integrated database and session management.

Basic example of AdonisJS,

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

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