Build a REST API from scratch using NestJS

What is NestJS?

Installing NestJS

npm i -g @nestjs/cli

Create a new project

nest new project-name
git clone project
cd project
npm install
npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata 

Getting Started

App Controller

App Module


Let’s Code

nest g controller items

Find all items

Get All Items — VSCode Rest Client
Get Item By ID
npm start
npm run start:dev

Create Items

POST Endpoint Test

Create Providers

nest g service items

Wrap everything in a Module

Deploy MongoDB Instance

docker pull mongo
docker run -p 27017:27017 --name mongodb -d mongo

Setup Mongoose

npm install --save @nestjs/mongoose mongoose
npm install --save-dev @types/mongoose
npm install --save @nestjs/config

Load Port Configuration


Inject Models

Using Models

Get All Items
Get Item By Id
Post Request
PUT Request
Delete Request
Get All Items

Logger Service

 nest g service logger
Logger Test


Source Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store