Building Modern Nodejs Application using Nestjs and TypeScript

In this article, we will see how to build REST API using Nestjs with TypeScript. Building Modern Nodejs Application using Nestjs and TypeScript

Recent Articles,

How to Run MongoDB as a Docker Container in Development

TypeScript Basics – The Definitive Guide

Crafting multi-stage builds with Docker in Node.js

Nestjs is a framework for building efficient,scalable node.js server side applications.

Under the hood, Nest makes use of robust HTTP Server frameworks like Express (the default) and optionally can be configured to use Fastify as well!

Why NestJS

Nest provides an out-of-the-box application architecture which allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications.

it provides design patterns out-of-the-box which helps to develop scalable application.


Here, i will mention some of the good resources that you can follow to get deep dive into nestjs

Setting up NestJS

First install nestjs globally on your machine to use the nestjs cli in your machine.

1npm i -g @nestjs/cli

now, you can able to use nest command globally. After that, you can create a new nestjs project using the command.

1nest new rest-nest-api

it will create a project scaffold. it will create lot of files for us to save time.

Project Structure

there are bunch of files that are predefined here. let's try understand one by one.

First and foremost, you can see a file main.ts in the application. it is a main entrypoint for an application.

1import { NestFactory } from "@nestjs/core"
2import { AppModule } from "./app.module"
4async function bootstrap() {
5 const app = await NestFactory.create(AppModule)
6 await app.listen(3000)

it will take the file app.module and create server instance by calling NestFactory.create and start running server on PORT 3000. we can change the port if we want to.


After that, app.module.ts. it will contains the group of components such as controller,services for the particular domain.

For Example, if we are designing REST API to handle User, User controller and User Service will be grouped in the user.module.ts and will be used in all other places.

app.module.ts will contain the App Controller and App Service

1import { Module } from "@nestjs/common"
2import { AppController } from "./app.controller"
3import { AppService } from "./app.service"
6 imports: [],
7 controllers: [AppController],
8 providers: [AppService],
10export class AppModule {}


Controller handles all the request and response and Service will have all the interaction between DB and controller.

1import { Controller, Get } from '@nestjs/common';
2import { AppService } from './app.service';
5export class AppController {
6 constructor(private readonly appService: AppService) {}
8 @Get()
9 getHello(): string {
10 return this.appService.getHello();
11 }

AppController will have decorator @Controller based on that nestjs identifies the controller and services.

we have method called @Get decorator which tells that it is a get method. if you pass the prefix in the @Controller,it will handle that particular route.

For example, if you want to handle the /user routes in the UserController, you can pass 'user' in the controller like @Controller('user).

Each method decorator such as @Get,@Post and @Put in UserController will be added with a prefix.


Services will be injected to the controller. Service contains all the Database queries.

1import { Injectable } from "@nestjs/common"
4export class AppService {
5 getHello(): string {
6 return "Hello World!"
7 }

let's run the application. To run it, we can use the command

1npm run start:dev

you can see the application running in terminal like

you can visit the url http://localhost:3000

Let's Build Some REST API

Let's create a CRUD for todo application in nestjs. Before starting to create a api for a domain, remember the flow of nest js.


Firstly, create a service for todo domain. you can generate the service using the following command,

1nest generate service todos

service command

After that, create a controller file using the following command,

1nest generate controller todos

controller command

Once those files are created, it will be added into app.module.ts automatically.

app module

Now, it is time to write some API for our application. To get all the todos data, we need a get request.

First create a get method in the todos.service.ts

1todos = [{ id: 1, name: 'Complete' }, { id: 2, name: 'Check' }];
3getTodos() {
4 return this.todos;
5 }

Once you add the function in service, import service in the controller to use that function.

1constructor(private todosService: TodosService) {}

Now, you can able to call the get method in the controller

2 getTodos() {
3 return this.todosService.getTodos();
4 }

it will return all the todos in the get request. you can verify it by running the application.

get api

Likewise, we will add the other api request such as POST,PUT and DELETE.


1import {
2 Controller,
3 Get,
4 Param,
5 Post,
6 Body,
7 Put,
8 Delete,
9} from '@nestjs/common';
10import { TodosService } from './todos.service';
12interface todosDto {
13 id: string;
14 name: string;
18export class TodosController {
19 constructor(private todosService: TodosService) {}
21 @Get()
22 getTodos() {
23 return this.todosService.getTodos();
24 }
26 @Get(':id')
27 getTodo(@Param() params) {
28 console.log('id',;
29 return this.todosService.getTodos().filter(t => ===;
30 }
32 @Post()
33 addTodo(@Body() todo: todosDto) {
34 console.log('todo', todo);
35 this.todosService.addTodo(todo);
36 }
38 @Put()
39 updateTodo(@Body() todo: todosDto) {
40 console.log('update todo', todo);
41 this.todosService.updateTodo(todo);
42 }
44 @Delete()
45 deleteTodo(@Body() todo: todosDto) {
46 console.log('delete todo',;
47 this.todosService.deleteProduct(;
48 }


1import { Injectable } from "@nestjs/common"
4export class TodosService {
5 todos = [
6 { id: 1, name: "Complete" },
7 { id: 2, name: "Check" },
8 ]
9 getTodos() {
10 return this.todos
11 }
13 addTodo(todo) {
14 this.todos = [...this.todos, { ...todo }]
15 }
17 updateTodo(todo) {
18 this.todos = => {
19 if ( == {
20 return { ...todo }
21 }
22 return t
23 })
24 }
26 deleteProduct(id) {
27 this.todos = this.todos.filter(t => != id)
28 }

After adding the following code, we can able to add todo and update it using the api's. you may noticed that i used interface in the controller.

NestJs uses interface to model the data use to check the type of data in the POST Body and other places also.

Basically, it is a common pattern that is used in Nestjs Application development


get apis updated


To conclude, this summarizes the basic concepts to run the Nestjs application and Building Modern Nodejs Application using Nestjs and TypeScript .

we will see some advanced concepts of Nestjs in upcoming articles.

Recommended Course to Learn TypeScript : Stephen grider's TypeScript Course

To Read More

I Accidentally wiped the entire dat...

One of the tragic accident in my job turned out to be good learning for me in re...

List of Docker Container Commands y...

This article covers list of commands that you should know to manage docker conta...

Everything you need to know about d...

Docker volume is a persistent data storage mechanism to store the data in docker...