Node Frameworks Comparesing

Aspects to consider

  • Size of the project

  • Code structure and organization

  • Typescript support

  • Validation

  • OpenAPI support

Options

Nest.js/Express.js

Express and Nest.js are both popular web frameworks for Node.js, and they have their own strengths and weaknesses. Here are some potential benefits of using Express over Nest.js:

  1. Simplicity: Express is a minimalist framework, meaning it has a small footprint and is easy to learn and use. If you're building a small to medium-sized project or a simple REST API, Express might be the better choice. Its simplicity makes it easy to understand what's happening under the hood, and it can be easier to debug if something goes wrong.

  2. Flexibility: Express is very flexible, which means it can be used for almost any type of web application, from a simple CRUD API to a full-fledged web application. It doesn't impose any strict architecture or folder structure on you, so you have the freedom to organize your code however you want.

  3. Large community: Express has been around for a long time and has a large and active community. This means you'll find plenty of resources, tutorials, and libraries to help you build your application. It's also easier to find developers with experience in Express than in Nest.js, which can be helpful if you need to hire additional team members.

That being said, Nest.js has its own advantages, including:

  1. Architecture: Nest.js is built on top of Express, but it adds a lot of structure and organization to your code. It's based on the concept of modules, which makes it easy to separate your code into reusable components. This can make your code more maintainable and scalable in the long run.

  2. TypeScript: Nest.js is built with TypeScript, which adds static typing to your JavaScript code. This can help catch errors early and make your code more readable and maintainable. If you're already familiar with TypeScript or want to use it in your project, Nest.js might be the better choice.

  3. Built-in features: Nest.js comes with a lot of built-in features, including dependency injection, middleware, validation, and more. This can save you time and effort compared to building these features from scratch in Express.

Ultimately, the choice between Express and Nest.js depends on our specific project requirements and preferences. If we value simplicity and flexibility, Express might be the better choice. If we need a more structured and organized codebase or want to use TypeScript, Nest.js might be the better choice.

Nest.js/TSOA

Nest.js and TSOA are both popular Node.js frameworks that can be used to build scalable and maintainable APIs. Here are some potential benefits of using Nest.js over TSOA:

  1. Modularity: Nest.js is designed with modularity in mind, which means you can easily break your application down into smaller, reusable modules. This can make your code easier to manage and test, and it can promote better separation of concerns. TSOA, on the other hand, is more focused on generating OpenAPI specifications and controllers from annotated TypeScript classes.

  2. Flexibility: Nest.js provides a lot of flexibility when it comes to choosing your preferred libraries, databases, and other tools. It has built-in support for a wide range of third-party packages and libraries, and it also integrates well with other Node.js frameworks such as Express and Fastify. TSOA, on the other hand, is more opinionated about the tools and libraries you should use, and it has a more narrow focus on generating API documentation and controllers.

  3. Scalability: Nest.js is designed to be scalable and performant, with features such as dependency injection, middleware, and built-in caching. It can also be easily deployed to cloud platforms such as AWS and Google Cloud. TSOA doesn't provide the same level of scalability and performance optimizations out-of-the-box.

That being said, TSOA also has some potential advantages, including:

  1. Simplicity: TSOA is designed to be simple and easy to use, with a straightforward API and minimal setup process. It can be a good choice if you're looking for a quick and easy way to generate API documentation and controllers from TypeScript classes.

Ultimately, the choice between Nest.js and TSOA will depend on our specific project requirements and preferences. If we value modularity, flexibility, and scalability, Nest.js might be the better choice.

Conclusion/Proposal

Nest.js

Although using pure Express might be fast to set up, due to it's functional design and lack of structure it's mostly designed for providing simple APIs and it might be not ideal solution for more complex backend solution. As a bare minimum, we would need to extend it by using some library for generating Open API specification from Typescript and some annotations. TSOA is a framework designed to do exactly that while also providing object oriented pattern including (route) controllers and services, even integrating IoC/DI support. However, it's not well documented full flagged framework and is driven by a smaller community and has number of limitations. Assuming we want to implement scalable, well organised architecture, we should go with Nest.

Example of possible folder structure:

In this structure, the src/ directory contains all the source code for the application. The app.module.ts file is the main module of the application, which imports and configures all the other modules and services.

The common/ directory contains common modules, middleware, validators, exceptions, and other reusable components that are shared across the application.

The modules/ directory contains modules that correspond to specific features of the application, such as authentication, users, products, and orders. Each module may have its own controllers, services, and other components.

The services/ directory contains services that are used by the modules, such as the database service for accessing the database and the API service for integrating with external APIs.

The utils/ directory contains utility modules, constants, and a custom logger that can be used throughout the application.

The main.ts file is the entry point of the application, which starts the Nest.js application and listens for incoming requests.

Last updated