Amplication explains why you should choose NestJS as your backend framework. Learn about the advantages of using NestJS and why the framework is such a great match with Amplication.
- Fast and able to handle concurrent requests at scale
- Straightforward and productive
- Reliable, widely supported, and well-documented
As I dug deeper and deeper, looking through pretty much the entire Node.js tech landscape, I encountered NestJS. A few things were immediately noticeable:
- It’s fast-growing, incredibly popular, and well-documented
- Built on top of battle-tested technologies (Express.js, Fastify)
- Has first-class TypeScript support
After reviewing NestJS with the team, we decided to go for it. I’ll give you a spoiler - we haven’t regretted the decision since.
After over a year of working with NestJS and being very satisfied, I wanted to share some of its most valuable strengths with you, and why you should consider choosing NestJS as your backend framework.
NestJS is an open-source project with 47 thousand stars on GitHub. It is well maintained with over 300 active contributors. New features, enhancements, bug fixes and security fixes are addressed swiftly.
Thanks to its large user base, there is a massive ecosystem of useful plugins, packages and integrations which saves us a lot of time and makes for an excellent developer experience.
According to the StackOverflow Developer Survey, TypeScript’s popularity is increasing every year. It has been in the top three most wanted and loved technologies in the report for several years in a row.
If you are already using TypeScript or are looking to migrate, NestJS is a no brainer!
NestJS is opinionated
To make the best out of NestJS, you should follow its common conventions and object-oriented ideology. While this is largely up to personal taste, I believe it’s one of NestJS’ most valuable strengths.
Embracing NestJS’ opinionated standards allows us to focus on delivering functionality and thinking less about trivial topics such as folder structure, file naming, variable naming, approach to testing and so on. Using an opinionated framework removes a lot of trivial process overhead that ultimately slows down developers and teams.
For this reason, all NestJS projects look and feel the same, which makes it easy for teams to collaborate.
The structure of an auto-generated NestJS project:
Robust module system
This is one of my favorite features about NestJS. NestJS’ built-in modules system naturally results in a neat separation of concerns.
A typical NestJS application is made up of several modules. Each module concerns its components (Controllers, Services etc.). Modules may depend on other modules.
This mechanism enables Dependency Injection, an incredible feature that helps us manage our dependencies and share them across our application without having to worry about duplication, initialization etcetera.
You can read more about modules here: NestJS Documentation - Modules
NestJS’ module system makes testing very easy. Modules should serve a single purpose (think stuff like Orders, Users, Products) and therefore are incredibly testable. Writing automated tests in isolation is simple with NestJS.
Upon creating a project, NestJS also creates some basic resources, including unit tests and end-to-end tests.
As you may already know, when writing tests, we often need to mock dependencies. The NestJS Testing Module has taken this into account and provides some auto-mocking capabilities to help us out.
The NestJS createMock function helps us auto-mock dependencies:
You can read more about testing here: NestJS Documentation - Testing
If you’re interested in diving deeper into testing and NestJS, there is an incredible GitHub repository containing testing recipes and examples for many use cases such as GraphQL, gRPC, and REST. Take a look: jmcdo29/testing-nestjs.
Security is a critical aspect when developing software today. A reliable security record can help us build trust with our users and customers. On the contrary, security issues may result in irrecoverable damage to our brand.
When using NestJS, this is one less thing for you to worry about. NestJS enhances security by providing easy-to-integrate authentication and authorization modules and guards.
On top of that, NestJS takes care of stuff like CORS and CSRF for you (yet still lets you configure it to your needs).
Naturally, you want some of your routes to be protected for authorized users only. NestJS has got you covered with its Guard feature, which makes it very easy to achieve. The usage of decorators makes things very declarative, which is a plus.
POST/products endpoint is protected thanks to NestJS AuthGuard:
For example, NestJS neatly integrates with Passport.js, a popular library for authentication and authorization, which enables you to introduce strategies such JWT (JSON Web Token) very easily.
Support for common technologies
Connecting to a database? No problem. Want to spin up a GraphQL API? Easy. Need to consume RabbitMQ? Absolutely. Fancy caching stuff on Redis? For sure!
NestJS’ growth rate and popularity has resulted in a rich ecosystem of packages (official and community maintained).
These packages are easy to install and integrate. They are implemented as NestJS Modules, which makes it very natural to integrate them and consume them within your application.
An example of how to easily set up a GraphQL API in your NestJS project:
Here is a short list of useful techniques, if you wish to explore further:
For more useful techniques, take a look at the NestJS Techniques page in the documentation.
Well documented, friendly community
Seriously, NestJS has some of the best documentation sites I have seen. It is so easy to get started.
The maintainers have done an excellent job at covering stuff like a solid introduction, explanations of core concepts (with visuals), fundamentals, common techniques, and recipes.
I highly encourage you to take a look at the NestJS Documentation and experience this for yourself.
On the community side, the NestJS Discord server is very active with over 30 thousand members. I have asked for help multiple times and was always met with support and understanding.
With NestJS being so approachable and popular, the community takes an active part in contributing to its ecosystem. It’s a win-win for all.
NestJS seems to have it all. It’s fast, it does not re-invent the wheel, it supports all popular tools and technologies and paradigms. It was built based on solid architectural paradigms, including a robust modules system, dependency injection and testability in mind. NestJS apps are very easy to get started with and extend with just a few lines of code.
NestJS truly removes a lot of overhead and helps us be more productive.
NestJS + Amplication = a match made in heaven
Here at Amplication, we believe in simplicity, efficiency, and open source development, so it was pretty much a foregone conclusion that we would be incorporating NestJS into our platform.
Our vision is to create a platform that auto-generates code for professional developers, eliminating repetitive tasks and boilerplate code. By using Amplication you code only what matters to the delivery of business functionality. Even so, you are still the captain, and it is you who gets to steer the ship.
Our project is open-source and our community is growing. Click here to check us out.