What is NestJS?
Hey guys, welcome back. In this video, we will talk about NestJS. So NestJS framework is increasingly becoming popular among developers who want to build scalable server-side applications. So those of you who don't know what NestJS is, you can go to this website which is nestjs.com. And according to this website, NestJS is a progressive Node.js framework for building efficient, reliable, and scalable server-side applications.
NestJS fully supports TypeScript, and in the background, it uses Node.js framework and Express. So what we are going to do is we are going to learn how we can use NestJS to build our REST application. Although NestJS supports many different protocols, for start, we are going to build a REST application using NestJS.
Now, NestJS has a very extensive and very good documentation. So if you are thinking about starting NestJS, you can click on the Documentation tab and you will be able to see tons of good documentation. So under the documentation, you can see what NestJS can do and what are the technologies it supports. So you can see under the techniques it can support authentication, database, configuration, validation, caching, so many things it can support. Also, it can support GraphQL, WebSockets, microservices like Redis, MQTT, RabbitMQ, Kafka. Also, it has the support for OpenAPI, Swagger UI and many other good stuff.
Also, if you want to see how popular NestJS is, you can go to the GitHub page for NestJS and you can see as of today it has more than 31,000 stars. So it's becoming really popular.
Prerequisites for Learning NestJS
So, let's talk about the prerequisites of learning NestJS. So knowledge of TypeScript and JavaScript will be very important for learning NestJS. If you have the experience of Angular, it will be a plus because it uses techniques which are already used in Angular, for example. Now, because we are going to be building a RESTful API, it's good to have a REST client such as Postman in order to test your REST application. Also, make sure you have the latest version of Node.js installed on your operating system. So once you have all these tools, we are good to go, and let's get started.
Installing the CLI and Creating a Project
So what we are going to do, we are going to just open our terminal and we are going to install some NestJS packages. So what I did is I created a directory. So I'm going to just go in that directory, which is called work.
So the first step is to install NestJS CLI globally in order to create our NestJS project with this CLI tool. So we're going to just give this command which is npm i -g @nestjs/cli. And in my case, because I want to install this globally, I need the sudo privileges. Also in your case, it might not be necessary. So I will just press enter and give my Mac's password, and now this CLI is installed on my operating system.
So once this Nest CLI is installed, we are ready to create our first project. So in order to create a new Nest project, what you need to do is you just need to give this command nest because we have installed NestJS CLI globally, this nest command is going to work. And then write new and the name of your project. So we are going to create a project called car-manager. So I'm going to give this name. You can give any name for your NestJS project and then press enter, and it's going to create a new NestJS project for you.
Now, first step here, it's going to ask you which package manager you want to use, NPM or Yarn. So I'm going to just use NPM for this. So I'm going to just press enter, which is going to create our project using this NPM package manager.
Exploring the Project Structure and Scripts
So now you can see our NestJS project is created and you can see it gives us a hint also that we can CD into this directory and then we can just run our project using npm run start. So what we are going to do, I'm going to just do ls, you can see this project is created inside this directory. So I'm going to go inside this directory and then I'm going to open this project using Visual Studio Code. You can open this project with any other other editor or IDE of your choice. I'm going to use Visual Studio code here so I'm going to just open this project using Visual Studio Code. And let me open the terminal also inside Visual Studio Code.
Now let's discuss about the NestJS project structure. So, you will be able to see this package.json where under scripts you will be able to see all these commands. So we can start our application using npm start, which is going to essentially run nest start in order to start our application. And you can see we can test our application using npm test. Under the hood, it's going to use Jest for running our test. You can also see how you can run different commands for running different kind of test and coverages, and running your application in different environment, for example, development or debug or production mode. You can also run lint for linting your application files.
The Application Entry Point: main.ts
Now let's talk about the source folder where our code is there. So we're going to start with the main.ts, which is the main entry point of your NestJS application. So here, you can see it's very simple. It's going to just import this NestFactory from the core NestJS package. And you just need to pass your AppModule, which is there inside this app.module.ts file. We are going to just see what's there in a bit, but you just need to import this AppModule and you just need to pass this AppModule as a parameter in the create method. And then you can just start your application on the port. For example, by default, it's going to start on the port 3000, but you can change this port also. So app.listen and it listens on the port 3000 by default.
Core Building Blocks: Modules, Controllers, & Services
So now we're going to go inside the module.ts folder. So app.module.ts, which we have imported inside our main.ts. So this app.module.ts file is going to contain the implementation of the application root module.
Now there are three important building blocks of a NestJS application. And these are called controllers, providers, and modules. So three building blocks: controllers, providers, and modules. And you will be able to see one file for each component. You will have the controller.ts file, you will have the module.ts file, and you will have the service.ts file. So services are also called providers in NestJS. module.ts is going to contain the implementation of your application's root module. And then app.controller.ts is going to contain the implementation of a basic NestJS controller with just one route. So by default, it's going to just listen on this GET route and it's going to just return you "Hello World!".
So controllers are used to just define your endpoints. So it will be a main interface for your application. So you can define endpoints into the controllers. Now, the app.service.ts file is going to contain the implementation for your endpoint, for example. So for example, some REST request comes into your application, it's going to just go here, and then this controller, in this controller you can see there is an instance of AppService and whatever you want to return as a response for that REST request, you can just provide the implementation of your response inside the service. So let's go inside the service. Here we just define the implementation about your service methods, right?
Now also, you will be able to see, for example, when you go to the app.modules.ts this kind of annota- here. So these are called the decorators. So if you are familiar with the Angular, you might have seen these kind of decorator decorators, right? So because we are in the module decorator here, we just provided @Module, and then inside the module, you can provide the imports, controllers, and providers. So provider essentially are the services, right? So we have imported the app.service and app.controller here. And here we provide the mapping of all the controllers you have inside your application and all the providers you have in your application. And sometimes you need to import different kind of files in your NestJS and those imports we are going to do here. So we are going to see more about imports in the later part of our application, right? So, as I said, you have services, modules, and controllers.
Also you have the app.controller.spec.ts file. So this will be your unit test file, so here you can write your unit tests. Okay? And you can also define, for example, service.spec.ts file for testing your services. And for your integration test, you have this test folder. So here you can run your integration test. And in NestJS, it is called end-to-end test. Okay? So here you can test your whole application using Jest and under the hood it uses SuperTest for your integration tests.
Running and Modifying the Application
So now let's run our application. You just need to write npm start here which is going to run our application. And once you see this message which says 'Nest application successfully started', you can go to your browser and then just write localhost:3000 and it's going to return 'Hello World!', right?
You might be wondering, this is our TypeScript project so we haven't transpiled our TypeScript into the JavaScript code. And how it's running directly using npm start? In the background, NestJS is going to take care of transpiling and then running your application directly using npm start. If you want to just explicitly build your project, you can give this command npm run build. It's going to transpile your TypeScript files into JavaScript, but if you directly use npm start also, it's going to transpile directly your files and it's going to run your project.
For example, I'm going to just stop this application. I'm going to go to the service.ts file, and here we have the getHelloWorld which is returning this "Hello World!" to us. I'm going to just add "Hello World from me". And then I'm going to just restart our application. It's going to directly transpile our TypeScript code and it's going to start our application on the port 3000. So let's refresh this page, now it says "Hello World from me."
Conclusion and What's Next
So, this is how you can create your first NestJS application. In the next video, we are going to see what we are going to be implementing. We are going to be implementing a REST API. So, we are going to see how we can create our own REST API using NestJS. I hope you have enjoyed this video and I will see you in the next video.