Skip to main content

NestJS

NestjsLogo

What is Nest.js?

NestJS is quite a popular and cumulative JavaScript framework functioning under the hood of Node.js and is used to construct scalable, reliable, and efficient server-side applications. The framework is channeled with the Node.js environment and supports TypeScript fully. It also can be scaled to make use of Express.js under the influence of Node.js. Since NestJS is a full TypeScript supported framework, it can enable developers like you to code purely in JavaScript and would let you combine the concepts of Object-Oriented Programming(OOP), Functional Reactive Programming(FRP), and purely Functional Programming(FP).

NestJS aims to provide a certain level of abstraction mechanism to the developers so that not only the common server-side frameworks like Fastify or Express that are integrated internally, but can also be exposed to the APIs chosen by the developer. These hidden bars provide developers like you to gain the freedom to use the third-party modules vividly and they can be made to underlie in the NestJS platform without having to manipulate the entire server-side.

Getting started

To check out the guide, visit docs.nestjs.com.

Installation

  • Download and install Node.js
  • Nest CLI installation on local computer
$ npm i -g@nestjs/cli  
  • Creating a project
$ nest new project-name

Project Structure

ProjectStructureNestjs

Platform

Nest aims to be a platform-agnostic framework. Platform independence makes it possible to create reusable logical parts that developers can take advantage of across several different types of applications. Technically, Nest is able to work with any Node HTTP framework once an adapter is created. There are two HTTP platforms supported out-of-the-box: express and fastify. You can choose the one that best suits your needs. Platform | Description -------|----------- platform-express|Express is a well-known minimalist web framework for node. It's a battle tested, production-ready library with lots of resources implemented by the community. The @nestjs/platform-express package is used by default. Many users are well served with Express, and need take no action to enable it. platform-fastify|Fastify is a high performance and low overhead framework highly focused on providing maximum efficiency and speed.

When you pass a type to the NestFactory.create() method, as in the example below, the app object will have methods available exclusively for that specific platform.

const app = await NestFactory.create<NestExpressApplication>(AppModule);

Details about the files

File nameContents
main.tsIt is the application's entry point. It consists of a method NestFactory.create() which creates a new instance of the Nest application.
app.modules.tsIt contains the module of the application's implementation.
app.controller.tsIt consists of only one routed implementation controller of NestJS.
app.services.tsIt is the usual service method implementation.
app.controller.specs.tsIt testes the file for the controller.

main.ts

pModule } from './app.module';  

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

NestFactory method is imported as an entry point of the application. AppModule is imported from the previously discussed app. module file. Secondly, the bootstrap is marked as async and implemented. The main purpose of importing the bootstrap function is to call it for the code execution. The execution takes when the NestFactory.create() method is called the AppModule is passed as an argument to the root application module. This will attach a module by creating a new NestJS instance. The next step is to start the server by using the event listener method on the webserver with port 3000.

app.modules.ts

import { Module } from '@nestjs/common';  
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

AppModule is imported as a method and @Module decorator is imported from @nestjs/ sharing common library. The @Module decorator when passed consists of three properties namely imports, controllers, and providers. The specified controllers in the AppModule should always be the part of the array assigned to the property in the initial stage of the application development because the count of the controller is constrained to AppController which is the root module.

app.controllers.ts

import { Controller, Get } from '@nestjs/common';  
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}

Controller in NestJS consisting of only one GET route. The class can be made controller just by adding the @Controller decorator. It is also imported from the @nestjs/ which is a common library for all. The point here is to understand clearly that the controller usually relies on a service class. In the examples given AppController uses Appservice to implement the app.services.ts file by importing the corresponding statement that is added at the top. However, the process of inserting AppService into AppController is based on the method of Dependency Injection which by default adds the parameter of the same type. A default route is then implemented to handle the HTTP GET request through @Get decorator to implement getHello method. The method is therefore powered by AppService to fetch data and carry out request and response actions by returning at the same time.

app.services.ts

@Injectable()  
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

Running the application

$ npm run start

Contribute

Nest.js is OpenSource. GitHub Repository for Nest.js

Nestjs Projects

There are several opensource projects built using nest.js which are available for contribution. Find the list here