A framework or programming is only as good as its fundamentals.
Once you become proficient in the fundamentals, it becomes easy to master the entire framework.
NestJS is no exception.
Without a solid understanding of the key concepts and principles that underpin the framework, it’s easy to get lost in the details and struggle to make progress.
? That’s the reason for this guide.
The foundation laid down in this guide will help you master the fundamentals of NestJS.
Ultimately, it will help you build complex applications and solve challenging problems as you progress in your NestJS learning journey.
So, grab a cup of coffee, sit back and let’s dive in!
1 – NestJS setup and platform choice
To kick things off, you need to learn how to set up NestJS and choose the right platform for your application.
NestJS is based on Node.js. And therefore, you need to install Node.js on your system.
After that, it’s a matter of installing the correct packages. Broadly, you have two choices – the CLI and manual setup.
Though the manual setup gives you more flexibility, I always recommend going with the CLI as it makes your life easy.
Once you are done with the installation, you can start building your first application.
At this point, you might need to make a choice between the Express and Fastify platforms. Either of them works great, but you should consider your specific requirements to choose one over the other.
Check out the below link for more detailed steps
2 – NestJS Controllers, Providers and Modules
The next step in understanding the fundamentals of NestJS is to learn about controllers, providers and modules.
In NestJS, controllers, providers and modules are the key building blocks of a robust and maintainable application.
? Controllers handle incoming requests and return responses. You use controllers to define the routes and endpoints of the application.
Learn more about them in this post about creating your first NestJS Controller.
? Providers are meant to share common functionality or data between different parts of the application.
Create your first provider with this step-by-step guide on NestJS providers.
? Modules help organize controllers and providers into logical groups and make it easier to manage the codebase
Learn how to organize your code base with NestJS Modules.
Also, make your modules configurable by creating dynamic modules in NestJS.
3 – Managing Exceptions in NestJS
Exceptions are a part of life for every developer, no matter which framework you choose.
The trick is to learn how to handle exceptions.
NestJS has an extremely robust exception-handling process that takes care of most things automatically. All you have to do is throw the proper exception and NestJS will take care of the rest.
? Here’s a step-by-step guide on exception handling in NestJS.
However, many times the default exception setup is simply not enough. There are cases when you might want to have complete control over the exceptions layer of your application.
For such cases, NestJS provides exception filters that allow you to customize the complete exception flow as well as the response that is sent back to the client.
? Check out this tutorial on how to create & use a custom exception filter in NestJS.
4 – Generating API Documentation in NestJS
Building an API is useless if no one knows how to use it.
And the best way to communicate this is to generate automatic API documentation using Swagger.
NestJS provides a special module to set up and configure Swagger for your application. Once done, it exposes the OpenAPI-compliant documentation for all the endpoints defined in your application controllers.
Swagger also gives you a special user interface where you can play around with the API routes by sending and receiving responses.
? Here’s a step-by-step guide on configuring Swagger with your NestJS application.
5 – Middleware functions in NestJS
Middleware functions in NestJS allow you to intercept requests before they are processed by the controllers. This makes them a core part of NestJS fundamentals.
The NestJS middleware functions are extremely similar to the ones you might have used in Express.
You can use them to perform tasks such as authentication, logging or error handling.
? Check out this post on creating and applying a middleware function in NestJS.
6 – Interceptors in NestJS
Interceptors in NestJS take the game even further than middleware functions.
You can use interceptors to modify the incoming requests or outgoing responses before they are processed by the controllers or returned to the client.
Interceptors let you implement aspect-oriented programming in NestJS. With interceptors, you can add extra functionality to your NestJS application without modifying the core logic, making it more flexible and easier to maintain.
You can use them for tasks such as logging, caching or error handling.
? Here’s a step-by-step tutorial on creating and binding an interceptor in NestJS.
7 – Pipes in NestJS
Pipes are another key NestJS fundamental concept.
In NestJS, pipes are used to validate and transform the incoming data before it is processed by the controllers.
You can use them for tasks such as data validation, sanitization or even transformation. With NestJS pipes, you can ensure that the data is in the correct format and meets the requirements of your application, making it more reliable and secure.
? Check out this post on NestJS Pipes with Examples.
? Also, you can learn how to use the in-built NestJS ValidationPipe.
NestJS is a pretty large framework with lots of moving parts.
However, getting well-versed with the fundamentals of NestJS builds a strong foundation for learning further advanced concepts.
In this post, I have summarized the core concepts of NestJS along with links to detailed posts.
Alternatively, you can also get a FREE e-book covering all the NestJS fundamentals with code examples in one single place. All you have to do is subscribe to Progressive Coder.
In case of any queries or comments, please feel free to write in the comments section below. If you found this post useful, consider sharing it with friends and colleagues.
NestJS is a fundamental pillar in my Cloud & Backend learning path. To know more, simply subscribe to the Progressive Coder newsletter.
Also, say ‘Hi’ on Twitter for more real-time updates on what’s happening at Progressive Coder