NestJS: A Backend NodeJS Framework
Join the DZone community and get the full member experience.Join For Free
Last week an idea stuck my mind to explore the frameworks available in the NodeJS ecosystem to develop backend APIs. I had been using ExpressJS for a long time, and I thought it was about time to see what alternative frameworks were like.
I started listing down all the features that I wanted in a good NodeJS framework:
- Easy to develop
- Integration with other applications
- Easy to test and deploy
- DevOps support ready
- Support for microservices
- REST and GraphQL
I took my special analyzer goggles, fuelled up my time machine, and started my journey to the future in search of a new framework. I went through frameworks like Koa, Hapi, Molecular, Seneca, etc. But none of them met all of my needs. Then, I saw NestJS.
Yes, you heard it right. I can change the underlying core framework to either Express or Fastify without changing any code. (Fastify has got a proven track record when it comes to performance.)
Let's have a look into how you can set up the NestJS.
You may also like: Nest.js Brings TypeScript to Node.js and Express, Part 1.
The installation is pretty straight forward. You can use npm/yarn to install the framework.
The cli will ask for the type of package system you want (npm/yarn). Once the installation is complete, you can start the application using the start script.
Open any browser and go to http://localhost:3000. You will now see a "Hello World!" message.
You can also use
npm run start to start in the application in dev mode. This will watch for any changes in your code and restart the application accordingly.
In most NodeJS backend frameworks, when writing an API, we will create a callback method for the defined URL. This is not the case in NestJS.
If you have a Angular/Spring Boot background, you will find the below features very familiar.
In NestJS, in order to create an API, we need a controller, which is similar to a component in Angular (or a RestController in Springboot).
Let's say I need to create a time API that returns the current server time. Run the following CLI command inside your project directory to create a new controller:
This will create two files. The time.controller.spec.ts is the test file and time.controller.ts is our main file
You can see that it updates the
app.module.ts file to add the reference to the new controller,
time.controller. This is similar to bootstrapping a component in Angular.
This is how the folder structure currently looks:
Open the time.controller.ts file and replace the code with the following block:
Restart the application (if you had started in dev mode, it will restart automatically), and access http://localhost:3000/time using Postman/browser /curl.
You will now see the following JSON response.
If you gave an argument in the
@Get decorator, then the API URL will change accordingly. So, the effective URL of an API should be "API" + @Controller's URL + @Get's URL.
@Get, NestJS also supports other REST verbs, like
@Delete, etc. Whatever object is returned from the method is sent back to the client as the result of the API. You can get more details about the controller implementation at https://docs.nestjs.com/controllers.
This is one of the simplest API implementations in NodeJS, similar to Java's Spring Boot implementation.
In NodeJS, we use try/catch or exception middleware to handle the errors in the application. But, in NestJS, the approach is unique. You can create a filter class that can handle the specific exceptions.
This means if an
HttpException is thrown from any part of the application, this filter is triggered, the logic executes, and a response is sent back to the client.
Easy? Huh? You can find more details at https://docs.nestjs.com/exception-filters.
Security is very critical. Typically in an ExpressJS application, we use middleware to check for the authorization level of an API. In NestJS, we can use guards instead.
and in the controller:
You can also create global guards in
One of the biggest advantages of this framework is its out-of-the-box support for microservices and transport layers. It has support for different transport layers, like TCP, gRPC, MQTT, RabbitMQ, etc.
You can read more about microservices in NestJS @ https://docs.nestjs.com/microservices/basics.
NestJS has out-of-the-box support for GraphQL. To start, install the GraphQL support package:
NestJS offers two methods for integrating GraphQL:
- Schema first: where the definition is written in GraphQL Schema Definition Language (SDL).
- Code first: where we use decorators to generate the GraphQL schema.
You can read more about GraphQL integration @ https://docs.nestjs.com/graphql/quick-start.
Express and Fastify
Another impressive feature of NestJS is that it can run on top of Express or Fastify frameworks, and you don't have to change your API's code for this. If you want to access any native framework features of Express or Fastify, it's pretty simple.
By default, NestJS uses Express. If you want to use Fastify, then you can install the package for Fastify, like so:
Benchmark results show that Fastify has superior performance when compared to Express.
IMHO NestJS is a framework that can be used in small backend applications to large enterprise-level applications. It helps you in building an efficient, scalable application. The maintainability of this framework is huge compared to other NodeJS frameworks.
I've seen projects using ExpressJS, where the code maintainability has become a serious concern after a few months of development. The ability to switch Express and Fastify is one of the cool features of NestJS.
Head on to https://nestjs.com to start developing your app.
Published at DZone with permission of Vinod Surendran. See the original article here.
Opinions expressed by DZone contributors are their own.