Hi everyone and welcome back. So in this video, let's take a look on NestJS CLI, how it works and how we can create a basic application. We will go through the basic application structure.
So this is a nestjs.com. It's talking about, okay, NestJS is a framework for building efficient, scalable Node.js server-side application that we already talked about. It fully supports TypeScript.
That is the advantage of using NestJS. Okay. You can see that the philosophy of nest.js is this has given a rise to awesome projects like angular react and view Because you can write the nest.js back-end very easily So how to get started with this you just need to install this globally first So if you already have then you can skip it and then project name So it is installing a nest js globally for on the system and then in the current working directory we can just create a simple application that application will have the proper structure what nest js is providing with the basic application so you will see nest js cli.json you will see a simple module a default module having a controller and services so let's take a break i think it will take some time okay now we can just say okay let's just new hello world okay so it will scaffold the application for us you can see what all it has created it has created a basic readme file and importantly nest cli.json it is type supported by typescript i mean it has a full support so you can see one tsconfig build tsconfig.json and rest all it has some test cases added unit test cases and e2 test case and one simple module with one controller and with one service okay installation is in progress i think it will be doing npm install and all before that we can just see the application how it is how it looks like you might have seen the same kind of code structure where in angular v Import everything in main.ts and then we bootstrap the project. Okay, same kind of thing here We are starting the nest.js module.
I mean bootstrapping the nest.js module and starting the application on port 3000 Here we have this is the root module Okay, an application in nest.js is giving us the modular structure Where you can create a multiple modules all the other modules will inject in the root module. So for us Whatever you are passing in the main.ts This will be the root module. So I have dot dot module dot aces root module. It has a controller.
It has a service. In the controller we will define the routes. So currently you can see it is actually whenever you are going to hit forward slash. So whenever you are going to hit forward slash http get then you will be able to get the message from the service.
Here you have a service and it is just returning hello void right. You might be seeing some annotations here right we are using annotation injectable annotation controller so these are actually building blocks for your next-gen application we have a controller we have a services so what is the entry point let's start with main.ts this is the entry point from here you will inject the root module of the application this is app module app module is a collection of your all the controllers all the providers all the services will be defined here Because we are bootstrapping only one module, one module will have all the other modules will be imported here. Like if you are using swagger module, SDS module, mongoose module that will be defining the imports. All the controllers, controllers are nothing but the routes you can see. Okay, I want to create a API forward slash users.
So I will be defining, creating one user controller, defining the routes there. Currently we have app controller and if you see the app controller it is having simple. http get here you can define the path okay I want like okay user hello okay so all the sub routes will follow that so instead of HTTP forward slash this route will be hello okay similarly you can define all the other methods here you can use these annotations If you want http post right then you can execute this method and here you just need to import it just need to change the method names okay this is how we will be creating our routes so controllers are pointing to the routes services will be actually the resource which will be getting the data and services will have injectables so You will be injecting this app service in the controller.
How we are doing it? It is simple. We are doing a dependency injection here inside the constructor.
You can see if you are familiar with angular in angular front end, we do this thing. We create a HTTP service or some kind of a data service. We inject that in the components. Similarly, in the next days, we are creating services.
We are injecting them into the controllers and controllers will from different routes. We'll get the data from these services and will respond to the user request. Okay, so you can see what all other things we have like eslint rcd.js. It is supporting all the ts lints. Parser options it is looking at the ts lint json.
Here we have defined ts config first. So in the ts config we have defined all the compiler options. The module type we are using common js.
and target is ES1017, source map is true. Whenever you hit npm run build, it will generate the output inside a dist folder. If you look at the package.json, here we have all the required scripts. You don't need to write anything. The cover is to do npm run test, npm run lint, npm run lint fix, npm run build.
Okay, everything is here. So you can do npm run build. So it will just generate a nest build.
Okay, I need to go inside a folder ready to just generating npm build npm run start to start this process Start the next project. Okay, and if you don't want it to do it, you can also use a node mode We will introduce that in our project using node mode We can just use a watcher or you can also do it in the watch mode and give them start there. This will actually take care of if any file changes then it will restart the nest js server for you okay start debug start proper and all these things start prod it is actually running the nest js project from compiled output so you see in the dist folder we can see all the compiled files so if you wanted to run it for production it is running the compiled code node dist main dot main dot js file is there You can see main.js. It is a compile output code.
You can decide how you want it to run your production. That is fine. You can also in production also you can just run npm run start. Next start.
Next will take care of compilation on the fly. OK. npm run format it is just using prettier ESLint and all. If you are doing a linting then it is using ESLint and applying the fix based on the rules you have defined in the ESLint.rc.
OK. Basic folder structure, these are the tests where we have written E2U tests. E2U tests are like integration tests where we are actually checking the endpoint or the API endpoint which you have written. We will also be writing the unit test cases where you will be checking each and every method of the controller, services, middleware, interceptors you are writing.
Next CLI, basic things like it is talking about semantics and the source root is SRC. ts-lint build configuration whenever you are doing whenever you are running the build npm run build it will be extending this base configuration i mean you can create a you can create a new ts config json by extending the existing one so i'm extending the ts config but in this case i'm excluding everything which is in the node modules test dist folder and spec.ts because i wanted to build only from the src folder except the test cases okay So now you can see my application is running here. If I show you, you can see I started Nest application and it has exposed these two routes forward slash hello get forward slash hello post. Okay, basic NestJS application, plain and simple using NestJS CLI we are able to run a basic application right now, instead of that you can start creating your own controllers own services on modules here you can create like okay I wanted to use user module. ok here you create your own user module.ts, user controller.ts and user-service.ts.
Once you have that module created, I can just copy and paste these things. Like I have a user module. I don't have a controllers and services created yet. So this is user module I have.
What I will do is, I will inject this module here. and the benefit of typescript is you can import directly things are good you now you have two modules user can have its own routes like i have a user controller i can just get it from here i can have my own controllers you can just replace it to a user controller forget about services for now we will create our own services later here Get hello user and here I can say post user right and I can just return simple hello nothing else. right so this is another controller i have created and the route will be user and i will be importing it in the user module and i have to just define it here user controller now it should be a part of my module now you can see these get and post are exposed right so this is how it works right this is a nice and clean way of writing the routes controllers services we'll talk about more about how all the other building blocks we have in nest.js which is of talking about interceptors, pipe, filters, middlewares all these are actually use useful building blocks which you will plug to your module so i have now totally two different modules one root module one user module user module can have its own interceptors filters pipe created inside the user module will have a dependency here and you will be importing this module user module into the root module app module okay thanks everyone