, NestJS Quick Start Guide |

NestJS Quick Start Guide

Nest (Nest.JS) one of popular frameworks for creating awesome Node.js server-side applications. Nest makes use of robut Node.js HTTP servers. By default it uses Express and if you want, can change  the HTTP server framework like Fastify.

Nest’s architecture is so similar with Angular. If you are a Angular developer, you will adapt to Nest, easly.

Ok, well, we can start to install Nest.


Nest CLI package should be installed as global. Then you can use it anywhere, where you want. Each Nest commands starts with nest prefix.

For creating a new project, select a folder in terminal and type nest new project-name.

After you create a new project it will ask you which environment you used

In this tutorial, I’ll go on with npm.

After you choosed your environment, Nest will install requiremend packages in to the project. Your project folder will look like this:

Your project folder will look like this.

We will works in src folder. There is main.ts. In this file we define our root module and start application with porth which we defined. Port is 3000 by default.


When you create a new project, it wil start a simle service, module and controller instance.


Controllers handle incoming request and return a response to the client. You can create a new controller with Nest CLI with typing nest g controller path/controllerName . It will be imported in to the nearest module.

In controller you can define routing paths, and define methods for each path seperatly.

I want to show two diffrent way to define post and get methods with a same result.

Expamle 1:

When you call http://localhost:3000/hello-world  you will get a text response as “Hello World!” and if you send a post this page with name param in request body (Forexample name=Jim) you will get a text response as “Hello Jim!“.

Expamle 2:

This example little diffrence from the abow. We define path as param on Post and Get decorators and pass empty to Controller and if you call http://localhost:3000/hello-world on browser or send a post, we will get same results. If we define paths on Controller and Get decorators both, like @Controller('hello')  and @Get('world') we should call http://localhost:300/hello/world to get response.

Services (Providers)

Nest’s services (providers) have same idea with Angular’s services. Providers can inject dependencies.  This provides comunicate between components, handle datas, storage datas, define jobs etc. during runtime. For create from Nest CLI type nest g service path/serviceName. As controllers, it will be imported in to the nearest module.

We are going to check Services example of Nest document. This service storage and retrieval data and it will be used by CatsComponents. This service should be imported in the same module with CatsComponents or any module exported it and imported by CastComponents’ module.


Now, we can use the cats service.


In the controller, CatsService is injected through the constructor. Thanks of CatsService, CatsControler can storage cats in service (provider) and can use them to return from Get request.


Modules allow us to orginize structure of the app.

Modules are annotated with a @Module() decorator. This decorator get a single object whose define module properties.

Fields of objects:

providersThe providers will be used in this modules and shared by it.
controllersList of controllers which are used by this module.
importsRequired modules list.
exportsShared modules and provider list by this module.



Posts Tagged with…

Write a Comment

Skip to toolbar