What is Nest.js?
Nest.js is a progressive Node.js framework. Let's learn about it.
Hello and welcome. I'm Dave. Today we're going to learn about Nest.js, and I'll provide links to all resources in the description below. I'll also provide a link for you to join my Discord server where you can discuss web development with other students. And you can ask questions that I can answer and receive help from other viewers too. I look forward to seeing you there. I'm excited to get started with this Nest.js series, so let's start off with a few common questions about Nest.js. And let's be clear, it's Nest.js and not Next.js, two separate things, of course.
Now Nest.js, a question that is frequently asked, is it the same as Node.js? Well, Nest.js is a framework built on top of Node.js, and so that is really the difference, kind of like Next.js is a framework built on top of React. Now when we talk about frameworks, they're often more opinionated. They have a structured way of doing things. so let's look at this next question: what is Nest.js versus Express.js that we would commonly use with Node.js?
Well, Nest follows the MVC design pattern, while Express doesn't do that. You can attempt to follow the MVC design pattern with Express, but Express is very unopinionated. And if I jump over to the expressjs.com, you can see it says it's fast, unopinionated, a minimalist web framework for Node.js. So, unopinionated is right in their slogan, whereas Nest.js is very structured and is opinionated, not unopinionated. So, when we come back here, we get a little bit more detail here, and it says we use components like controllers, providers, and modules. We'll be covering all of those things.
So finally, I want to answer one other question: why is Nest.js so popular? And we're looking at Google results for all of these, so you could look these up as well. But it's opinionated, as I said, it's well-structured architecture, it has a definite way that you do things. It has TypeScript support, so we will be using TypeScript right from the very beginning. Scalability, meaning you could of course scale this up, make a larger application, and because of that structure, it won't turn into spaghetti, it won't get disorganized. And it has a strong community, so you can look up a lot of things about Nest.js on the web already.
Prerequisites for Learning Nest.js
Let's go to the Nest.js documentation, and here we can see, I have it highlighted, under the hood, Nest makes use of robust HTTP server Frameworks like Express, and Express is the default. So not only is Node under the hood, but so is Express. And that brings me to prerequisites. You're going to understand much more of this Nest.js series and the benefits that Nest.js provides if you're already familiar with working with Node.js and Express. And I have a course for that on my YouTube channel, Node.js for Beginners, where you'll learn both of those.
Also, Nest.js has TypeScript built-in, so we'll be using TypeScript right from the start here. And besides that, if you're not familiar with TypeScript, of course, I have a course for that on my channel as well. Besides that, there will be a lot of OOP, which is object-oriented programming. It's not all OOP, it does say Nest has some FP, which is functional programming, or FRP, which is functional reactive programming, but much of what you're going to see is OOP. And that is creating classes, extending classes, adding methods, calling methods, things like that. So if you're also familiar with JavaScript classes, it will help. Maybe go back and brush up on those. So I could say this is Nest.js for Beginners, but that doesn't mean absolute coding beginners. That means you should know these prerequisites to get the most out of this series before you dive into learning Nest.js. And our series goal is going to be building a fully functional REST API with Nest.js. So now let's jump into VS Code and get started.
Project Setup and CLI Installation
Before we get started in VS Code, you do need to have Node.js installed. If you don't, go to nodejs.org and install the LTS version that is recommended for most users. That stands for long-term support. So go ahead and download and install that if you don't have it. We can check your version back in VS Code.
Okay, I've got VS Code open. I have an empty folder, and that's what you should do, is open up an empty folder in VS Code that you're going to create your project in. I'm going to press control and the back tick to open a terminal window. Now just to verify that you have Node installed, or a fairly current version, you can type node -v and check what version you have. I have 18.18.2. I saw there was a new version there, so I need to install that very soon as well. However, 18.18.2 is well beyond the requirements that we need to work with Nest.js, so that's where I'll start today.
Now after that, Nest.js has a very useful command line interface that we want to install for our project first. We'll type npm i -g @nestjs/cli. This will install the command line interface globally, that's what this -g does. And you want that so you can just use it anytime you're starting a new Nest.js project. So I'm going to press enter. It will begin installing. It could take just a little bit of time, and then when it's finished, we'll come back.
Okay, the CLI is now installed. So now I can type nest new and then the project name. Now I'm going to type lesson01 because I'm going to keep different folders or directories, if you will, for each lesson in this video series. You can name this whatever you want to, maybe nest-rest-api because you'll continue to probably build in that same folder. I'm going to say this is lesson 01 and press enter. Now it's going to ask which I want to use: npm, yarn, or pnpm. I'll just press enter on npm, and it will begin installing. Again, this will take a little bit, and I'll come back when it's complete.
Exploring the Starter Project Structure
Okay, that install has completed. And now I want to open up this lesson one folder and whatever, of course, you named your folder. So I'm going to go to the file directory, choose open folder, and from there I'm going to choose this Nest series folder. I've got, there it is, lesson 01, and select folder. And now we have opened up the new project that we created with the CLI. It was that easy. Lots of files over here. We're going to look in the source directory, and you can see here the files have the TS extension. We are using TypeScript. Let's look at the main.ts and you can see it really just starts everything off here. We are having our app listen on port 3000. This is something if you're familiar with Node.js and Express already, that part should look kind of familiar right there. Everything else probably looks a little different, but this is the entry point, this is what kicks off your application.
So now if we look at the rest of the files here beside the main.ts, we can see an app.module.ts. Now this is the main module, the root module for our application. And you can see it specifies imports, it also specifies controllers and providers, and it's already added a controller here. We can look at that. Here's our app.controller.ts. Then it has a .spec.ts for the controller, this is where we would write tests for that controller if we were doing that. And then there is an app.service.ts. This is what has the provider that is talked about right here, the service. So a service provider if you will.
Now if we break this down, and we look at the controller, and we're bringing in AppController from our controller file here. You can see the controller has a route, so this is a get route. And then it has the getHello, or here specified as well. So what we do here is at the get route, we have getHello, and then we return the service that calls the method. So you can see how this is broken up. If you've created a REST API before, especially with Node.js and Express, you'd see the controller is more or less handling the route, and then what we have here in the service is the actual method that returns "Hello World!". So in the controller, we're importing the service, and then when we go to the get route, that is when this occurs that calls the service with the method here, and then that method is called. Now if that doesn't make sense, that's okay. We're going to dive in and create one of these step by step as we go.
Running the App and Testing the API
Let's look at the package.json because there's going to be a lot in here, a lot more than you would typically see if you just started a Node.js and Express application. And you can see dependencies listed here, and then lots of Dev dependencies as well. And of course we said we're already using TypeScript and we have lots of types here also. There's some things from Nest.js including the CLI listed here under Dev dependencies. The dependencies themselves, not that large of a list, and that's okay. But what we really want here is the start, and then there's also start:dev. We'll be using start:dev because notice it has a watch flag, so as we make changes to our files, it will restart our application and of course reflect those changes. So again, start:dev. And you may not be used to that, you may just be used to seeing Dev over here.
So now we want to open a terminal window again with control and the back tick. Here we can type npm run start:dev and it should start our Nest.js application. Of course that has the hello world provided. And here we can see everything logged already to the console as it started. So now let's go ahead and close out of this terminal, and let's go back to our module that we were looking at. This is the root module for the application and remember, now when a get request comes in, we can look at the controller. We can see that it handles a get request here, and we're going to have getHello and this is going to return this getHello method from the app service. And if we look at that method, it should say "Hello World!". So now to do all of this, let's go to my Thunder Client. You could do this with Postman or something else if you wanted to. I'm going to clear out this users message and just do localhost:3000. And now this is a GET request, and I don't need a body there or anything, but it won't hurt if I leave it there as well. But this is just going to localhost:3000 as you see right here. And I'm going to send this request. And now it returns "Hello World!" and that's exactly what is expected.
Generating a New Module with the CLI
So now let's create our own module, something we want to use here besides our basic root module that returns "Hello World!". And of course, we could have it do anything else we want to. Now to do that, we can use that useful Nest.js CLI once again. So control and the back tick. Now I'm just going to leave it running here, and I'm going to open up another terminal window with the plus symbol. You could do either, you could press control C to close out of the application or exit the application and you could of course do this as well.
What I'm going to do now though is type nest g module and after that I need to type a module name. Well, I'm going to make a users module, so I'll just type users and press enter. Now we should see this add a users directory inside of this Source directory here to the application. Remember this one is our root here that we have app.module. So now let's open this up and I'll close the terminal windows once again. And inside here, we see a users.module.ts. Now it doesn't have many of the other things that were inside of this app.module yet, but we'll be able to add those as we need them.
One important thing to note that was added to our root module, the app.module.ts, is now in imports, where it was previously empty, now it is importing the UsersModule. It's creating that relationship. So this would be the parent, if you will, it's the root module, and of course the users module does link back to that. Now let's look at the users module again, and we can see we don't have much here. Previously the other module was specifying things like imports, controllers, and providers here. So let's put controllers and for now, let's put an empty array. And after that, let's put providers and for now, let's put an empty array as well. And we won't need to add much more here except when we provide the controller, when we create one, we'll need to put it in here and import it, and the same for the provider.
Generating a Controller and a Service
So now to create our hierarchy here with the provider and the controller, let's go ahead and open the terminal once again. Now I'm going to type nest g controller and now I'm going to type users to create the user's controller. So we'll press enter, and we should see not only the controller created, but it will also create that file for tests. I can close the terminal and click on both of these. Here we have the controller and here is the file that ends in .spec.ts where we would write tests for the controller.
Now let's open a terminal once again. I'm also going to type nest g service and this will create our provider. Once again, we need to provide a name which again should be users. We're creating all of these for our users. Now that I did that, we have two more files. I'll close this out, and we have the users.service.ts and we have the users.service.spec.ts that would have tests once again. So now we see the structure. And now if we look back at our module, without myself even adding those, remember I had empty arrays right here, now we have both of those imported and they are provided here to the module inside of the brackets that we had previously left empty. So now we have a complete module for our users and we're ready to move on and put in the routing logic in the controller and then eventually the logic for handling each of those routes inside of the provider that will then be injected into the controller.
Conclusion and Next Steps
So all of that is coming up, and we'll start in on the controller in the next lesson. Remember to keep striving for progress over perfection, and a little progress every day will go a very long way. Please give this video a like if it's helped you, and thank you for watching and subscribing. You're helping my channel grow. Have a great day, and let's write more code together very soon.