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 a while. Therefore, there are many interesting alternatives worth at least knowing about.
::: This does not mean that we have to stop using express.js
. In fact, it works perfectly. It’s simply interesting to stay updated on some of the options available. :::
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 Koa example
import Koa from 'koa';
const app = new Koa();
app.use(async (ctx) => {
ctx.body = 'Hello, Koa!';
});
app.listen(3000);
Hapi
Hapi is a robust and highly configurable web framework, ideal for large and complex applications.
Some standout 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();
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 efficient HTTP request handling
- 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}`);
});
NestJS
NestJS is a framework that uses TypeScript and is inspired by Angular. Although it is more oriented toward full-fledged web applications, it offers:
- Module-based architecture and decorators
- Easy integration with external libraries
- Support for building 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();
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 speed in development
- Integrated 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);
Download the code
All the code from this post is available for download on GitHub