How to set up a NestJS server?

Ajinkya
The StickyBit
Published in
3 min readMay 30, 2020

An overview of what Nest is, how it works and a step by step guide for setting up a Nest server.

Image credits- https://nestjs.com/

This blog is a guide to create a NestJS server. We will also walk you through the basics of NestJS if you are unfamiliar with it. If you know what NestJS is feel free to jump to server creation steps.

Let’s brush up the basics,

NestJS according to the official website-
A progressive node.js framework for building efficient, reliable and scalable server-side applications.

Let’s simplify it for you-

If you had a look at our previous blog,

you must have noticed that there is no defined structure for node.js backend application. You will need to invest your time in defining an efficient architecture. But guess what? NestJS saves this time of yours!

NestJS can be termed as a structured and more efficient form of node.js. Also, it is fully built with typescript, yet it takes advantage of the latest javascript features.

NestJS does so many things behind the scenes. It wraps controller in a try-catch block, error handlers, middleware, parses the request body and so many other things. It also gives you the option to choose an API framework such as express and fastify. Developers have to write Controllers, Services and Models. We will cover these terms ahead.

Let’s start with creating a server-

Step 1- You should have node.js installed. You can refer to our previous blog to know how to install node.js.

Step 2- To install NestJS, open terminal and fire the following command.

npm i -g @nestjs/cli

Nest provides you with a command-line interface and a set of commands to let you create modules, controllers, services etc.

Step 3- Fire the following command to create a new project.

nest new projects-name

Go inside the project directory we just created and fire the following command.

npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata

This will install the libraries which are essential for development.

Step 4- Go inside the project folder and fire following command

npm run start OR npm run start:dev

The second command will start the server in development mode i.e. once you save file changes the server will restart automatically.

This will start our Nest server on port 3000

Terminal preview after running the command npm run start:dev

Step 5 (Final step)- Open your browser and type in http://localhost:3000 and you will see a “Hello World” response from the server.

In the below image I have modified the response coming from app.service.ts file.

Browser Window

And we are ready with our Nest server!!

Module, Controller, Service and Model

Module- Module is an entry point of any component we create. app.module.ts is an entry point of any Nest application.

Controller- It is a collection of application APIs which handle incoming requests and return responses.

Service- Service file contains a database connection and is used to communicate with a database (CRUD operations).

Model- As we all know, a model is a representation of a real-time entity. In this context, a model represents a database table.

For more detailed information on this you can refer to official documentation.

Companies using Nest

Image Credits: https://docs.nestjs.com/discover/companies

In our next blog, we will cover CRUD operations and database migrations using Nest.

If you face any problems in setting up a server, let us know in comments and we will try our best to solve your queries. Stay tuned for the next blog. Till then, keep learning! Cheers!

--

--

Ajinkya
The StickyBit

Javascript Enthusiast, Fullstack javascript developer