Explaining NestJS In Depth

Introduction

This would be a series of articles, in this article, we would talk about out-of-box files code explanation and implement new custom APIs (to be covered in the next article)

It will cover the following things,

  • What is NestJS? (covered in the previous article)
  • Background (covered in the previous article)
  • Installation (covered in the previous article)
  • Hands-on Lab – Create a sample NestJS project (covered in the previous article)
  • Hands-on Lab – Create custom API in the existing NestJS project (covered in the second article)
  • In-depth Explanation - Out-of-the-box files (current article)

In-depth Explanation - Out-of-the-box files

Open Visual Code and see the project structure with the following files,

Explaining NestJS In-depth

main.ts

  • In the below code, the NestFactory method is imported as an entry point of the application. Next, the 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 code execution.
  • Then, 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 web server with port 3000.
  • This method will return a promise indicating that the server has successfully started embarking on the await key.

Explaining NestJS In-depth

app.module.ts

  • In the below code snippet, the 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.
  • And, the services should be made readily available in AppModule and hence be listed in the array assigned to the property of providers.

Explaining NestJS In-depth

app.controller.ts

  • The below code snippet is a simple demonstration of a controller in NestJS consisting of only one GET route. The class can be made a 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 App service 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.

Explaining NestJS In-depth

app.services.ts file

  • The below code snippet is a simple demonstration of a controller in NestJS consisting of only one GET route. The class can be made a 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.

Explaining NestJS In-depth

In this article, we talked about all the out-of-the-box files in detail and create a few custom APIs

Stay tuned for the next series of articles, hope you see you guys again soon.

Happy learning!