Comparing different Node.js frameworks for your next project

header image

Javascript is a great language for server-side development as it comes with a lot of frameworks and tooling to make our lives easier. Node.js has many frameworks to choose from when it comes to API development. The most popular options for servers include REST or GraphQL. In this write-up, we will go over some options for developing APIs for projects of all scopes. Please note that this focuses mostly on REST API development.  We’ll compare Express.js, NestJS and Routing-Controllers.

Express.js

TLDR: Suitable for all types of projects, but it is unopinionated which may result in spaghetti code if the developers are inexperienced. If you’re starting out, use this but you’ll want to move to other frameworks after some level of experience.

Being one of the most popular frameworks for node.js, Express.js is a great option for developing REST APIs for all kinds of projects. It has a rich ecosystem of plugins and third-party tools which help increase developer productivity and allows you to focus on the core business logic. This means that you can do rapid prototyping and development.

Pros:

  • Very popular and has a great community, so you can expect your issues to be resolved very quickly if you reach out.
  • Huge ecosystem of third-party packages to facilitate development of common tasks like file upload, rate limiting etc.
  • Express middlewares allow layers of abstractions to be built around the core business logic and extract out common functionality.
  • Unopinionated – meaning that you have the freedom to use any kind of project structure you’re comfortable with.
  • Great typescript support and documentation.

Cons:

  • No strict project structure is enforced – which means that if the project grows in scale, it may get difficult to manage for newer developers.
  • Express does not come with a lot of built in functionality required for any mid to large-scale project such as logging, CORS, job processing etc. Due to this, you’ll often find yourself writing a lot of boilerplate code repeatedly. However, this can be mitigated by using generators such as create-express-app.

Note: People often argue that ExpressJS is slower as compared to frameworks like Koa or Fastify as the latter can handle more requests per second (fastify benchmarks). While there’s no denying the fact that ExpressJS brings some overhead, I think it is a fair tradeoff because of the rich ecosystem of plugins that are available for ExpressJS. Also, Node.js is a single-threaded language which means that it is a poor choice for CPU intensive workloads. If your server is constantly crunching numbers or doing some CPU intensive tasks, I’d recommend you ditch Node.js entirely and go for some other languages like Go, Rust, Java, C++ etc.

Routing-Controllers

TLDR: Suitable for all types of projects (generally mid to large) and enforces some level of project structure which helps new developers keep their code organized. It uses class-based controllers with heavy decorator usage and is built on top of Express/Koa.

It is a relatively newer framework as compared to Express.js, but I think it’s a great choice for new developers because it enforces some of your code to be structured while at the same time offering the freedom to organize most of your code as per your comfort.

Pros:

  • Easy to learn and get started.
  • Built on top of Express.js/Koa, so you’ll feel right at home if you know these frameworks.
  • Enforces use of typescript.
  • Concept of middlewares, interceptors, router guards and controller inheritance results in readable code.
  • Great documentation.

Cons:

  • Smaller community – although it is not a dealbreaker because the framework is very simple.
  • It may be overkill for smaller apps.

NestJS

TLDR: Suitable for large scale projects and microservices. Highly opinionated which results in strict code structure to be followed. Can also be used to develop CLI or standalone apps. Not suitable for inexperienced/new developers.

NestJS is a complete package and offers almost everything you’ll need out of the box when it comes to server-side development. It also uses a class-based pattern like Routing-Controllers, but it heavily relies on Dependency Injection and Controller-Service-Model pattern. It is built on top of Express/Koa but heavily abstracts the underlying functionality.

Pros:

  • Strict project structure – this can help developers write standardized code and understand other NestJS applications easily.
  • A complete framework with excellent documentation – which means that you’ll not have to leave the official documentation very often to get what you need.
  • Can be used to make any kind of APIs - It abstracts transport level details from the business logic. Once you write a controller, it can be easily connected to a REST API, Websocket API, Redis PubSub, RabbitMQ etc.
  • Great community support – you can expect your queries to be answered relatively quickly.
  • Comes with a lot of built-in tools such as logging, job processing (using bull), file upload (using multer), SSE, configurations, validation (using class-validator).
  • Concepts such as exception filters, pipes, guards, interceptors make it easier to organize code and extract out common functionality to reuse across the app.

Cons:

  • Very opinionated – which means you’ll often have no freedom to organizer the code the way you want.
  • Inexperienced/new developers may find it overwhelming to learn so many things just so they can get started.
  • The module -> controller -> service structure may be confusing at first. You’ll often get stuck at doing simple things like importing a service from some other module if you don’t read the documentation properly. Even if you do, you might get stuck trying to figure out the best possible way to organize your code. This will come with experience, but keep in mind that you’ll struggle at the beginning.
  • It’s an overkill for small scale apps. In fact, I’d argue that even for a medium scale app, you might be better off using some other frameworks.

Note: All the cons that I’ve highlighted have to do with the complexity of code organization and the nature of the framework. All these can be overcome with practice. I think NestJS is a great framework, but I’d recommend new developers to first try out Routing-Controllers and then move to NestJS. This is because Routing-Controllers can be thought of as a simplified version of NestJS which can help you get used to the class based pattern. You will also have a lot of freedom with Routing-Controllers so it will help you ease in to the overall structure patterns used in NestJS.

Overall, all frameworks are good for their specific use case and individual skill level. I hope this helped you decide what framework you can choose for your next Node.js projects. If you have any questions, feel free to comment below.

Share :
Tags :