
Explore how NestJS mirrors Angular for backend development, using modules, controllers, services, and decorators to design a rest api with MongoDB via Mongoose, including JWT-based auth and decorator-driven validation.
Set up the Nestjs in practice development environment by cloning the one-start branch, installing front end and back end dependencies with npm, using node and an IDE such as Webstorm.
Set up a NestJS server from scratch by bootstrapping via main.ts, defining AppModule, and creating a hello world endpoint in the rest API folder, then run on port 9000.
Create your first NestJS REST endpoint by building a courses module and a hello world controller with a get endpoint at /api/hello-world. Structure modules and controllers with async endpoints.
Implement the first rest endpoint for courses in NestJS, returning a data list from a shared course entity at /api/courses, and enable hot reload with ts watch and npm scripts.
Debug your nestjs backend with chrome dev tools by attaching the chrome debugger to a node process in inspect mode, set breakpoints, and test endpoints like /api/courses.
Set up a cloud MongoDB database with the MongoDB cloud service, creating a free Atlas cluster and an administrative user. Whitelist IPs and explore collections and documents.
Learn how to populate a MongoDB database for a Nestjs app using populate db script, connect via a MongoDB connection string, and query courses and lessons in a one-to-many relationship.
Learn how to use Mongoose to query MongoDB, define schemas for courses and lessons, and balance schemaless flexibility with client-side schema enforcement to prevent bugs.
Set up mongoose in a NestJS backend, define a course schema to enforce data validity, and integrate the mongoose module for CRUD operations on the NestJS course database.
Implement a NestJS repository with Mongoose to fetch courses from MongoDB, creating a courses repository with a find all method and dependency injection using the inject model decorator.
Implement a NestJs put endpoint to update a course by parsing the id with @Param and the changes with @Body, returning the updated course.
Implement repository update in NestJS to modify a course and return the updated document. Use findOneAndUpdate on Mongoose with the course id and partial changes.
Demonstrates implementing the delete operation for courses in a NestJS app with MongoDB, wiring the frontend to an http delete, and using Mongoose deleteOne in the controller.
Demonstrates creating a course via a NestJS HTTP post to /api/courses, accepting a partial course, saving with Mongoose, and returning the full course with its id.
Learn how to implement nestjs error handling in the update course method by validating input, preventing overwriting the course ID, and returning a 400 bad request via HttpException.
Learn to customize nestjs error responses with filters at method or global levels and test error handling using the Restlet browser client to simulate and inspect 400 bad request.
Learn to implement NestJS exception filters to customize HTTP error responses, build an HTTP exception filter, and apply it globally or to controllers.
Explore how Mongoose schema validation differs from SQL, including casting semantics, ignoring unknown fields, and enforcing required fields, then implement a NestJS exception filter to return clearer error responses.
Implement a global fallback exception filter in nestjs to catch uncaught errors, log them, and return a 500 response with a helpful message for mongoose schema validation errors.
Validate incoming JSON payloads at the API boundary using NestJS pipes and class-validator decorators, turning interfaces into classes and returning a complete error payload.
Introduce NestJS pipes as chained transformations that convert http body values, turning a string into a number for sequential number field, with type checks and mongoose config to avoid deprecations.
Learn to build a custom NestJS pipe that converts strings to numbers, handles validation, and plugs into the update course operation using parse int pipe and class validator.
Apply class-validator decorators to validate incoming Http payloads in a NestJS app, enforce a global validation pipe for full course instances, and tailor error responses for validation failures.
Configure the NestJS validation pipe to format and return validation errors with a custom validation exception, an exception factory, and a validation filter for clear 400 responses.
Build the course screen and connect it to a backend endpoint by URL, add a find-by-url method in the controller and repository, and handle 404s while prepping the lessons endpoint.
Define a lessons schema to model a one-to-many relationship with courses using mongoose, linking each lesson to a course via object id, enabling view course screen with a paginated table.
Implement a NestJS back-end endpoint for fetching lessons with pagination using @Query to read courseId, sort order, page and page size. Validate inputs and prepare to query the database.
Finish the lessons controller by implementing a NestJS repository to query MongoDB, fetch paginated lessons for a course, and expose a search with course ID, sort, page, and page size.
implement authentication and authorization for the nestjs backend using json web tokens and a login endpoint with secure password hashing, salt, and bearer tokens.
Implement a NestJS login controller and authentication module to receive email and password from the frontend, handle post requests to /api/login, and route successful logins to the courses page.
Implement login in the NestJS authentication controller by reading email and plaintext password from the http post body with the body decorator; query user model and 401 unauthorized if needed.
Learn to complete NestJs authentication by validating user passwords with the password hash and salt package and issuing a Json web token as a bearer token.
Implement a login flow that creates and signs a json web token with user email and roles, using a secret, to prove identity and enable authorization.
Store the jwt in local storage after login, delete it on logout, and attach it to all http requests with an angular http interceptor that adds the authorization header.
Learn how Nestjs middleware authenticates user requests by decoding the Json web token, storing user info on the request, and applying protection to the courses and lessons endpoints.
Implement a NestJS get user middleware to decode and verify a JSON Web Token, attach the user profile to the request, and bypass unauthenticated requests.
Protect find all courses endpoint by implementing a NestJs authentication guard with canActivate that reads the Http request for a user from a Json web token and denies unauthenticated access.
Implement a NestJS authorization guard that reads user roles from the request, configures allowed roles per endpoint, and grants access or returns a 403 forbidden error.
Apply an administrators-only guard in NestJS by extending the authorization guard to protect data modification endpoints; demonstrate with create, update, and delete actions on the courses controller.
Discover how NestJS, a node-based backend framework with decorator-based validation, uses controllers and repositories and secures endpoints with JWT, guards, and role-based authorization in a Mongoose MongoDB setup.
This Course in a Nutshell
If you don't know NestJs, it's like Angular but for the backend. With it, we can write our backend using the same concepts and object-oriented APIs that we already use to structure our Angular frontend: components, services, modules, pipes, etc.
NestJs is also quite similar to popular libraries that you might already be used to, such as, for example, the Spring library in the Java ecosystem, or ASP.NET MVC for the C# ecosystem.
Course Overview
In this course, you are going to learn from scratch how to design and develop a NestJs backend that will take the form of a REST API that queries a MongoDB database using the Mongoose ODM (Object Document Mapping) library. We will be providing a complete CRUD example showing how to perform the most common REST data retrieval and data modification operations using NestJs.
No prior NestJs, MongoDB or Mongoose knowledge is assumed as we will explain everything from scratch. We will cover all the essential NestJs concepts such as Modules, Controllers and injectable services.
We will learn in detail how to validate business data using a decorator based approach (with decorators like @IsString(), @IsPhoneNumber(), etc.) by leveraging the built-in NestJs Validation pipe.
We will also implement using NestJs commonly needed backend security functionality such as User Authentication, RBAC (Role-Based Access Control) Authorization, error handling, and cover more advanced NestJs concepts such as Filters, Pipes, Guards, interceptors and middleware.
Table of Contents
This course covers the following topics:
Introduction to NestJs
Running NestJs in Hot Reload and Debug mode
Writing our first NestJs REST endpoint
NestJs Modules, Controllers and injectable Services
Introduction to MongoDB
Setting up a cloud MongoDB database
Introduction to the Mongoose ODM library
Querying MongoDB from a NestJs REST endpoint
Complete CRUD example with NestJs and MongoDB
NestJs Filters and custom error handling
NestJs Pipes, building a custom pipe
Data Validation with class-validator and the Validation pipe
NestJs Middleware
User Authentication with NestJs
NestJs RBAC (Role-Based Access Control) Authorization
NestJs Guards
What Will You Learn In this Course?
After taking this course, you will feel comfortable designing and developing applications using NestJs. You will have learned everything that you need to know in practice for designing and developing production-ready secure backends using the NestJs framework.