Why I choose NestJS for a SaaS API

Brad Beighton
5 min readJul 29, 2023
NestJS logo

In the rapidly evolving world of Node.js, developers are constantly seeking robust and scalable solutions to build modern web applications.

This was exactly the position I found myself in when rearchitecting our SaaS application. I considered all the common options, Express.js, Koa.js, and Restify. We also looked outside of NodeJS to the likes of Django and Flask with Python and even some Rust frameworks like Rocket. At the end of the day, NestJS really hit all of the requirements. The top requirements were:

  • Be highly scalable.
  • Modular structure.
  • Built-in support for GraphQL

Why I choose NestJS

There were a few reasons why I choose NestJS which I’ll go into below, these are by far not all the reasons however are definitely the top few.

  • Scalability and Maintainability:

NestJS promotes a modular architecture inspired by Angular’s design principles. It allows developers to easily organize applications into modules, components, services, and controllers. This modularity helps with code maintainability, and scalability and allows us to isolate and refactor specific parts of the application easily, without affecting other areas. This is important as it allows teams small (like mine) or large to work on different features and functionality in parallel.

  • Native Support for Microservices:

When re-architecting our API we knew that Microservices was the route we wanted to take, so finding a framework that supported this type of architecture was important. Finding one that natively supported it was even better. NestJS has a handy package@nestjs/microservices that handles all the service communication and supports a range of transport protocols, such as gRPC which was my chosen protocol. I’ll write a deeper dive into this in another article.

  • Similarity to Angular:

At Ricoh Europe we already have strong experience and foundations with Angular, which meant getting started with NestJS was much easier than the other options. Any Angular developer can easily get started building a NestJS application with only a small learning curve. This is a big advantage when getting started building a new API. When rearchitecting our SaaS application we had tight deadlines and being able to get up and running quickly was important.

  • TypeScript at its Core:

NestJS uses TypeScript at its core which gives a strong adherence to TypeScript. TypeScript is great for many reasons that I won’t go into now, but some of the reasons why this was important for us was type safety, robust codebase, and early error detection.

  • Native GraphQL Support.

NestJS has a built-in module for GraphQL called @nestjs/graphqlwhich means it is easy to seamlessly integrate GraphQL into your application. This module allows you to define GraphQL schemas using decorators and TypeScript classes, making it easy to create and manage your GraphQL API.

Native support is important as you can be more confident that what you want to achieve is possible, also a supported NPM package is always great to use!

  • Middleware and Guards:

Also similar to Angular, NestJS offers middleware and guards, which allow us to intercept incoming requests and apply pre-processing logic before they reach the controllers.

Middleware can provide logging functionality, while guards enable you to implement authentication and authorization mechanisms with ease. Within our API Guards are used as authentication and authorization mechanisms. We find chaining guards provide better and more flexible functionality.

  • Active Community and Rich Ecosystem:

NestJS has a robust community that actively contributes to its growth. The ecosystem includes various plugins, libraries, and third-party packages that extend the framework’s functionality.

This extensive support ecosystem ensures that developers can find solutions to most use cases and accelerate the development process. There is also extensive documentation and even enterprise packages available. Depending on the type of developer you are and the team you have, this could be valuable as this could provide accelerated development to help you meet your deadlines. This isn’t something I have personally used so I can’t comment on how good it is.

  • Seamless Integration with nx.dev Workspace:

NestJS seamlessly integrates with nx.dev, a powerful development tool for mono repo-based projects. nx.dev provides essential features like code generation (I actually use this instead of code generation tools like Angular CLI), dependency graph visualization (I use this all the time to understand circular dependencies), and continuous integration support.

By leveraging the capabilities of nx.dev, developers can efficiently manage large-scale applications with multiple projects, modules, and shared libraries. I’m going to be writing a full article about nx.dev and how my team uses it in both our API and Web projects.

Is it suitable for everyone?

Whilst I personally think NestJS is great, I’m not blind to the fact that it’s not suitable for everyone. A few things straight away jump out to me.

  • Learning Curve:

While NestJS borrows architectural concepts from Angular, developers with no prior experience in Angular might find the learning curve steep. Embracing the framework's full potential and understanding its intricate features may require additional time and effort.

  • Complexity in Smaller Projects:

For small-scale applications, NestJS's modular structure and adherence to best practices may introduce unnecessary complexity. The overhead of setting up the project and maintaining the modular structure might not be justified for simple use cases. There are lots of other frameworks that are much more suitable for smaller projects.

  • Performance Overhead:

Though NestJS provides robust features, some developers may be concerned about the potential performance overhead compared to more lightweight frameworks. This concern becomes relevant when building extremely high-performance applications or microservices that require minimal resource consumption.

Performance overhead isn’t currently something I’m experiencing however I expect that if you don’t have good development practices it would quickly become evident.

  • Limited Adoption in Legacy Projects:

NestJS's emphasis on TypeScript and the modular structure might not be readily compatible with existing projects based solely on JavaScript or older frameworks. Migrating legacy projects to NestJS might require significant refactoring and integration efforts.

I’d only recommend using NestJS on a brand new API which for most people isn’t an option.

Conclusion

NestJS has emerged as a powerful and innovative Node.js framework, offering a plethora of advantages to developers building modern web applications. The emphasis on TypeScript, modularity, dependency injection, and native GraphQL support provides a solid foundation for scalable and maintainable projects.

However, the framework might not be suitable for every project, particularly smaller applications or legacy systems where the learning curve, integration challenges, and time to build outweigh the benefits.

Ultimately, the decision to adopt NestJS should be based on the specific needs of the project, the team's expertise, and the long-term vision for the application's growth.

Hopefully, this article will help people weigh up whether or not to choose NestJS for their API project an insight into my pros and cons.

In my opinion, NestJS is a perfect option for teams who already have Angular experience and are looking to build a large backend application, maybe with multiple microservices.

However, for smaller APIs, I think it brings unnecessary bloat and something more lightweight like Koa.js or even a Rust API would be more appropriate. For existing legacy APIs where re-building from scratch isn’t an option, this also wouldn’t be appropriate.

Drop me a follow or Subscribe to me for more like this or email me with anything you want me to talk about.

--

--

Brad Beighton

I am a Solution Architect for a global workplace management SaaS product. I craft innovative solutions that enhance productivity and user experiences.