NestJS is a framework that is written on top of ExpressJS and it is written in Typescript.NestJS is a progressive Node.js framework for building efficient, reliable and scalable server-side applications. The framework fully supports TypeScript and under the hood it makes use of the Node.js framework Express.
Installing NestJS
Before you can use NestJS to create your back-end application we need to make sure that the Nest CLI (Command Line Interface) is installed on your system.A reasonable alternative is to use the npx program (or similar features with other package managers) to ensure that you run a managed version of the Nest CLI.This can be done by using the Node.js Package Manager NPM in the following way:
`$ npm i -g @nestjs/cli`
Once the installation of the Nest CLI is completed the nest command will be available. You can use this command in the following way to initiate a new NestJS project:
$ nest new my-nestjs-01
Executing this command creates a new folder my-nestjs-01 in the current location and downloads the default project template into this folder.
You can also download the Starter Project from GitHub and use it.
You can also use npx:
$ npx nest new my-nestjs-01
Project Structure
The initial structure of the project consists of the following folders and files:
The most important folder of the project is the src folder. This is the place where you’ll find the TypeScript files which is application code inside. Later, when implementing our first example we’ll spend most of the time working in this directory.
Inside the src folder you can find five files in the initial project setup:
main.ts: Entry point of application. By using the
NestFactory.create() method a new Nest application instance is created.
app.module.ts: Contains the implementation of the application’s root module.
app.controller.ts: Contains the implementation of a basic NestJS controller with just one route.
app.service.ts: Contains the a basic service implementation.
app.controller.spec.ts: Testing file for controller.
Running The Application
Once a new project is created, we can use the below command to run the application.
$ npm run start
Finally you should see the message “Nest application successfully started”. This message informs you that the server is ready and you can try sending the first GET request to the default endpoint by simply using the browser and opening URL http://localhost:3000 and you will get Hello World! as a response.
Adding A New Endpoint
The endpoint which we’re going to add should accept the HTTP GET, POST, and DELETE requests and will be used to manage data of online courses. The following endpoints will be created to cover those requirements:
/courses — Endpoint accepting HTTP GET requests to retrieve a list of all available online courses
_/courses/[courseId] _— Endpoint accepting HTTP GET request to retrieve the online course with a specific course ID
/courses — Endpoint accepting HTTP POST requests to add new courses
/courses — Endpoint accepting HTTP DELETE request to remove courses. The course which should be removed needs to be specified by it’s ID which is added to the request by using a query parameter
Creating A New Module
Create a new module:
$ npx nest generate module courses
Executing this command is adding a new file to the project: /src/courses/courses.module.ts:
Creating A New Controller
Let’s add a new controller to CoursesModule by using the following command:
$ npx nest g controller courses
The main controller implementation is available in file courses.controller.ts:
Setting Up A New Service
Data access will be management by a service, so the next step is to generate a service class by using the nest command again:
$ npx nest generate service courses
This command is adding a new file courses.service.ts to the project
Get Courses
The two service methods getCourses() _and g_etCourse(courseId) are being implement to retrieve data:
The getCourses() method is used to return the complete list of courses.
The getCourse(courseId) method is retrieving just one single course by its id.
Add Course
This method is used to add new courses to the courses array.
Delete Course
The method deleteCourse(courseId) is implemented to remove a specific course item from the list of courses.
Updating CoursesController:
Top comments (0)