Cover image for How I learned NestJs

How I learned NestJs

joelvinaykumar profile image Joel Vinay Kumar Updated on ・3 min read


I wish you good day. Thank you for reading this. I'm a beginner & may mistake at some point. Be sure to comment below & we can have an open discussion.

This is first part of series "How to build nestjs API with mongoose and graphql"

Not a long topic, but I wish to keep it simple and brief.

Before we jump in, I assume you already have an idea of NodeJS, ES6 & has it installed on your machine.

Part I:

What is NestJS?

It is a framework to build server side applications. It combines the power of functional & object oriented programming in one. Thereby provides robust yet scalable applications. It has a ton of features built in & is also capable of custom plugins.

For a beginner like me, it is much similar to Node+ExpressJS setup but with OOP & Typescript. NestJS can use express or fastify. You need to define it explicitly which to use.

How to install?

To install nest:

sudo npm i -g @nestjs/cli
(global npm installation needs superuser priveleges)

To create a new nest project:

nest new <project-name>

A basic nest project structure:

   - app.controller.ts
   - app.module.ts
   - app.service.ts
   - main.ts

I won't go deep with "NestJs project structure", but this is something you should understand to further advance in this series.

NestJS comes with facility to write unit tests and do end-to-end testing as well.The main core lies in the src folder where all the magic happens.

How nest carries a request?

This is an example of how a request is processed

Alt Text

Controller class will route the request according to the method.
Service class will manage the business logic and does the math.
Module class is responsible for importing all dependent modules, classes & bind them together.

This will ensure we can quickly develop modules in organized fashion and readable code.

If you encounter some line of code like @Injectable() are decorators, it will basically just inject dependency modules into your class so you don't need to bother importing all those same modules every time. If you can, just do a web search about dependency injection. It is a great read.

If you're new, just do npm start and run the server. Open your browser and navigate to http://localhost:3000 and try to make sense of the controller, service structure.

import { AppService } from './app.service';

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

  getHello(): string {
    return this.appService.getHello();

@Controller routes the incoming request with base url ending with / to it's particular method.
In our case, it is GET and function to be called is getHello.
This function invokes the method called getHello in app service class.

Alt Text

Image credits: Santosh Yadav

Now, I'm bluntly assuming you have a basic idea of how nest can produce a web app. You can peep into package.json file for all scripts to run the application.

If you don't want to reload your server for every change, you can run server in watch mode like this :

npm run start:dev

Or simply just start:

npm start

Posted on by:


markdown guide