Introduction to NestJS
Introduction
Nest (NestJS) is a framework for building efficient, scalable Node.js
server-side applications
It uses progressive JavaScript, is built with and fully supports TypeScript and
combines elements of OOP (Object Oriented Programming), FP (Functional
Programming), and FRP (Functional Reactive Programming)
https://docs.nestjs.com/
Installation
Prerequisite
Make sure that Node.js (version >= 12, except for v13) is installed on
your operating system
Installation Command
npm i -g @nestjs/cli
Creating New Project
nest new project-name
Creating New Project
Command output
Creating New Project
Open the directory created by the project name -
course-manager to access the generated files
A basic controller
with a single route
The unit tests for
the controller
The root module of
the application
A basic service with
a single method
The entry file of the application which
uses the core function NestFactory to
create a Nest application instance
Running the application
yarn run start
Running the application in Watch mode
To watch for changes in your files, you can run the following command
yarn run start:dev
Testing the API with REST Client
You can use REST Client VS Code extension to test APIs
Testing the API with REST Client
Create a file named api-requests.rest under src directory
Add the following line in the api-requests.rest file
GET http://localhost:3000 HTTP/1.1
Click the Send Request link shown above the above line
You should see the API response in a different tab as shown in the next slide
Testing the API with REST Client
Modules
A module is a class annotated with a @Module() decorator
The @Module()decorator provides metadata that Nest makes use of to
organize the application structure
Modules
Each application has at least one module, a root module
The root module is the starting point Nest uses to build the application
graph
The application graph is the internal data structure Nest uses to resolve
module and provider relationships and dependencies
Modules
The @Module() decorator takes a single object whose properties describe
the module
Modules
The @Module() decorator takes a single object whose properties describe
the module
providers
will be instantiated by the Nest
injector
they may be shared at least
across this module
Modules
The @Module() decorator takes a single object whose properties describe
the module
controllers
the set of controllers defined in
this module which have to be
instantiated
Modules
The @Module() decorator takes a single object whose properties describe
the module
imports
the list of imported modules
that export the providers
which are required in this
module
Modules
The @Module() decorator takes a single object whose properties describe
the module
exports
the subset of providers that
are provided by this module
and should be available in
other modules which import
this module
Controllers
Controllers are responsible for handling incoming requests and
returning responses to the client
Controllers
Routing
The routing mechanism controls which controller receives which
requests
HTTP Requests
Http Requests contains the following information about the request
Request Method
GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Request Path
Path or Query Parameter
Request Body
Request Header
Controllers
Controllers provides mechanisms to access the information contained in
the HTTP request
They are responsible for handling incoming requests and returning
responses to the client
Controllers
The controller shown below handles
GET /customers request
Controllers
Request object
You can get request details using @Req() decorator
Controllers
Request object
The request object represents the HTTP request and has properties for
the request query string, parameters, HTTP headers, and body
In most cases, it's not necessary to grab these properties manually
We can use dedicated decorators instead, such as @Body() or @Query()
Controllers
Status code
The response status code is always 200 by default, except for POST
requests which are 201
We can easily change this behavior by adding the @HttpCode(...)
decorator at a handler level
Controllers
Headers
To specify a custom response header, you can either use a @Header()
decorator or a library-specific response object (and call res.header()
directly)
Controllers
Redirection
To redirect a response to a specific URL, you can either use a
@Redirect() decorator or a library-specific response object (and call
res.redirect() directly)
Controllers
Route parameters
In order to define routes with parameters, we can add route parameter
tokens in the path of the route to capture the dynamic value at that
position in the request URL
Controllers
Asynchronicity
Nest supports and works with async functions
Controllers
Request payloads
Providers
Providers are a fundamental concept in Nest
Many of the basic Nest classes may be treated as a provider – services,
repositories, factories, helpers, and so on
The main idea of a provider is that it can be injected as a dependency
this means objects can create various relationships with each other
the function of "wiring up" instances of objects can largely be delegated
to the Nest runtime system.
Providers
Service
The @Injectable() decorator
attaches metadata, which declares
that CustomerService is a class that
can be managed by the Nest IoC
container
Providers
Using the service
Dependency injection
The CustomerService is
injected through the class
constructor
Middleware
Middleware is a function which is called before the route handler
Middleware functions have access to the request and response objects,
and the next() middleware function in the application’s request-response
cycle
The next middleware function is commonly denoted by a variable named
next
Middleware
Middleware functions can perform the following tasks:
execute any code
make changes to the request and the response objects
end the request-response cycle
call the next middleware function in the stack
Middleware
You implement custom Nest middleware in either a function, or in a class
with an @Injectable() decorator
The class should implement the NestMiddleware interface, while the
function does not have any special requirements
Middleware
Applying middleware
Modules that include middleware have to implement the NestModule
interface
We then use the
configure() method
Middleware
Functional middleware
use it within the AppModule
Middleware
Multiple Middleware
Global middleware
Other Concepts
Exception filters
Pipes
Guards
Interceptors
ORM
Authentication/Authorization
References
https://docs.nestjs.com/
https://www.youtube.com/watch?v=GHTA143_b-s
https://www.youtube.com/watch?v=uAKzFhE3rxU
https://github.com/nestjs/nest/tree/master/sample