Introduction to NestJS

Edison Devadoss
YavarTechWorks
Published in
3 min readAug 6, 2022

--

Hi friends, In this blog, I will share my experience with NodeJS and NestJS.

I have been working as a backend developer for the last 3 years. I primarily developed applications using FastifyJS and ExpressJS.

Now I started to learn NestJS. I will share some advantages of NestJS.

Scalable:

When we are working on enterprise applications and when more developers are working on a single project at the time it is hard for all developers to write the same style of code and file creation. Consistency and maintenance became hard. For this case, Since NestJS is a full-fledged framework it ensures scalability and consistency.

Controllers with API:

In FastifyJS or ExpressJS, we define the route(API endpoint) first and then call the controller as a callback. But in the NestJS, controllers come with an API endpoint.

@Post('signin')
@Header('Authorization', 'none')
signin(@Body() dto: AuthDto, @Res() reply: Response) {
return this.authService.signin(dto).then((user) => {
reply.header('Authorization', `Bearer ${user.token}`);
reply.status(HttpStatus.ACCEPTED).send(user);
});
}

In the above code, we defined POST API with controller function.

Three main Concepts (Modules, Controllers, Providers):

Important concepts in NestJS is Module, Controller and Service or Provider.

Controllers are for receiving HTTP requests and returning responses.

Providers are for our business logic. A provider can be injected as a dependency. The provider file should have @injectable() annotation. Then only this provider can be injectable into the other services or controllers.

Modules are for structuring our code. Each application has a root module. NestJs consider this as a starting point of the application’s structure and relationships.

Amazing Command Line Interface(CLI):

NestJS provides powerful CLI, we do not need to create manually files for modules or services and configure them in the root modules. If we create a module using CLI it automatically adds it to the root module.

$ npx nestjs g service user

Using the above code we can create a user service and automatically configure it in the user module file.

Pre-build dev Environment:

NestJS provides a pre-build developer environment like Eslint configuration. We do not need to worry about Eslint's configuration. If we want we can change the existing configuration we can do it. We do not need to worry about the set-up for building the project or running the project, it comes with project creation.

More Incredible features:

NestJS provides some more incredible features like Pipes, Guards, Interceptors and etc. There are powerful features.

NestJS has good documentation.

Conclusion

From my understanding, I am not saying all the backend applications we can use NestJs. It is totally based on our situations.

If we are new to the backend development and not aware of the stuff like decorator, interceptors, and other angular-related things, at the time we can start with ExpressJS or FastifyJS.

When we are developing applications using ExpressJS or FastifyJS we need to work on under the hood, so the developer needs to write more code and understand more things deeply. But In NestJS things like JWT Token, we need to do configuration set up only. Once we completed the set up we do not need to worry about verifying the token, it automatically handles it. Yes, I agree we can customize the features based on our needs.

So I prefer If we are new to the backend development, first we can get familiar with ExpressJS or FastifyJS. Even behind the NestJS, we have to use either ExpressJS or FastifyJS.

If you have some experience in backend development then you can learn NestJS.

Thank you for reading. Have a nice day!

--

--

Edison Devadoss
YavarTechWorks

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader