No, you do not need Express in your REST API on Node.js

Original author: Rolando Santamaria Maso
  • Transfer
image

About alternatives to Express, where and why it is worth refusing from Express and small measurements in translation under the cut


Express


Express is a simple, well-documented, well-supported and most downloaded framework for Node.js.
If you try to google how to make a simple HTTP server, then you will most likely receive the following code with the first request:

const server = require('express')({})
   server.get('/', (req, res) => {
       res.send('Hello World!')
});
server.listen(3000);

REST APIs


In the architecture of applications developed by companies, the REST API and REST services continue to play a fundamental role, since HTTP is still used as the main protocol for communication. This means that a programmer who is going to make a new application or a micro service will use the REST API instead of, for example, an “event queue”.

With the advent of the use of micro-services, the REST API seeks to be as small as possible and perform the minimum number of operations. This entails that the number of end points in the developed API will increase in direct proportion to the number of micro services and for each simple action there will be its own end point. For example, in order to change user data in the database, the / user / search method will first be called to find the user ID we need, and then the / user / update method will be called with the parameter returned to us before this ID, instead of order to perform this operation (Note. Ed.).
Yes, the REST API must be fast, they must be fast!
Also, when creating a high-load application using the REST API, the micro-services pattern is increasingly used. Such applications are built on API Gateways. API Gateway is essentially a proxy server that a user is accessing, and this proxy server is already accessing the least loaded micro service. API Gateway usually also solves small tasks, such as:

  • SSL certificate handling
  • Load distribution
  • Authorization and Authentication
  • Caching
  • Compress request content
  • ...

However, Express is too heavy and slow.


Express is great, packed with features ... but it is also heavy and slow to use for small purposes, such as the REST API in micro services.


The latest version of the library (4.16.4) depends on 30 modules that are built into it, and in the development process about 20 more are added to this module, which ultimately makes the application too heavy to use as a micro service.

image

Compared to other libraries, Express is too slow to use for minimal purposes when it is not necessary to perform complex queries. The graph below shows a comparison of the execution of the simplest request for a JSON document.

image

image

The measurements shown in the last picture do not show that Express is slow in general, they show that you need to double the performance of your processor in order to perform the simplest operations ... As a result, the monthly payment for using AWS, Google Cloud, MS will increase Azure or another cloud service that you use, and this will turn into a larger and often unreasonable expenditure of funds.

Conclusion


image

In Node.js there are many different ways to implement the REST API, below which are suitable for you, depending on the task:

  • If you need maximum performance, but at the same time minimal costs, then Express is not exactly what you need. Take a closer look at Fastify .
  • If you need a small library with the minimum necessary set of functions, then you should definitely take a look at restify , koa , polka or restana

Also popular now: