Series & Guest Instructor Introduction
All right then gang, so welcome to this series where you're going to learn all about Nest.js, which is a framework for building Node.js applications. Now to teach all about this, I'm going to hand you over to Marius who is an awesome content creator here on YouTube. He's got loads of Nest.js content on his channel as well as tutorials about a whole bunch of other things like React, Vue, GraphQL, etc. So definitely, definitely check out his channel and subscribe to it. The link is going to be down below the video.
Anyway, without further ado, I'm going to pass you over to Marius so you can learn how to make an API with Nest.js.
An Overview of the Nest.js Framework
Doing a guest post for Sean here today, I'd like to share with you the Nest.js framework, which I would argue is one of the best frameworks for Node.js, especially if you're trying to build an API or other types of server-side applications. It's got a great set of features, awesome documentation, and a massive community behind it. If you take a look at their GitHub page, you'll see that they have over 50,000 stars there.
In this video series, I'll introduce to you all the core fundamentals that you need for Nest.js to get you started in building your own applications.
Why Choose Nest.js Over Express?
Now, before we get started, let's quickly talk about why Nest and what even is Nest? As some of you may know, a very popular framework in the Node.js ecosystem is Express. It's been around for quite a long time now, but by design it's meant to be very minimalist. It introduces to you a few concepts that let you build APIs. However, for the most part, the overall architecture is up to you, right?
If you take a look at their docs, all they really have here is, 'Hey, here's how you do some routing, some basic middleware and error handling,' and then that's pretty much it. This lack of architecture means that it's extremely flexible, you can pretty much do whatever you want with Express. However, as your project or team grows, you'll find that things can very easily turn into spaghetti. You really need conventions and structure; those are very important, especially when you need to start integrating with things like GraphQL or REST or Swagger. How do you do those things? It kind of leaves it up to you to figure it out.
Architecture, Features & Project Preview
Now, where Nest comes into the picture is that basically they say, 'Hey, let us take care of the architecture piece. We'll give you the guidelines on how to architect your server-side application.' And the best part? You still get to use Express under the hood. So you can kind of imagine that Nest.js kind of sits on top of Express. It simply introduces an out-of-the-box architecture, gives you a little bit of guidelines, and the best part is that because it's sitting on top of it, it means that all the things that the open source community has built for Express.js, a lot of that is still ultimately usable in a Nest.js ecosystem.
If we briefly take a look at the Nest.js documentation, you can see that there's a couple of core concepts here that we'll cover in this video series. But above that, you'll see that they have guidelines here on how to do a lot of extra things like how do you do testing, and how do you do database connections, and task scheduling, queues, and events, websockets, GraphQL. There's a lot of things here.
I should also mention Nest.js is very big on TypeScript. As you can see here, combining lots of elements from OOP, functional programming, you'll see that there's a big emphasis on decorators, which you might be familiar with from other frameworks like Spring in the Java ecosystem.
So that's Nest.js at a high level. There's definitely a lot to learn, and we're going to go ahead and create our own application so that you can learn those fundamentals. We're going to build a Net Ninja API that, think of it as an interface to manage your own army of ninjas. So kind of random, but also hopefully kind of fun.
Project Setup with the Nest.js CLI
All right, so to get started, open up a terminal. Hopefully you've got Node and npm installed already. I'm going to go ahead and install the Nest.js CLI. The CLI is basically, think of it as a way to help you generate new projects. It also has a couple other extra commands that I'll show throughout the series.
To create a new application with the Nest CLI, you just have to use the nest new command like this, and then provide your own project name. I'm just going to call this one ninja-api. Hit enter and it's going to ask you what package manager do you want to use. I'll select npm here, but feel free to use whatever you like. And as you can see, it's going to start creating a bunch of files for me and then it'll start installing my dependencies. From here, I'm going to go ahead and open the Net Ninja API in VS Code.
Running the App & Making a Request
All right, so here's the new application we just generated. I notice that there's a couple files in here that we'll talk about in a sec, but go ahead and open package.json, which is where you'll see that there's a bunch of scripts in here, commands that you can run in the terminal for the different things you can do with this application. So for example, how do you start the application? I will use this in a sec to start that, which is starting the application in watch mode. How to build, how to format all your files using Prettier, testing, etc.
So let's go ahead and try that start:dev command in the terminal. And it should output some stuff in here, and it'll say 'Nest application successfully started.'
Now, if we take a look in the main.ts file here, which is, think of it as the entry point for our application, you can see that it's listening to port 3000. You can change that though if you want. So let's go ahead and make an HTTP request to that port. I'm going to do a new request here using Thunder Client. It's sort of like a Postman that you can use right in VS Code. So I'm going to do localhost:3000, HTTP GET, and if I hit send here, you'll see that we get 'Hello World!'
So let's take a quick look at the code that we have to see how is it set up to respond with this 'Hello World!' message.
Tracing the Request Flow
Back in the main.ts file, you can see that the application is being made off of this app module in here. And if we go into that, that module has a couple things in it: controllers and providers. We'll dive deeper into what those things are in the following videos, but let's just take a look. Within AppController, you can see that there's a method here that is probably getting invoked. It says getHello and that ultimately also invokes an AppService that itself has its own getHello. If we go into that, you see that we have the AppService returning the 'Hello World!' message.
So if you kind of think about it, the request on / is basically just going to our controller, and then that forwards to our service, and then it ultimately bubbles back up in the other direction to give us our response of 'Hello World!'. So there's not much code here yet, but you can already see sort of a high-level architecture that Nest.js is telling you to use. For example, controllers are defining the routes and what HTTP verbs are we using for those routes, represented by methods, and then those methods ultimately call services. Which services will have your business logic and any sort of reusable logic that you want to use across whatever other services or other controllers that you have.
Modifying the API & Course Preview
Let's go ahead and maybe just change this to something else to prove that we can change the response. So maybe let's do 'Hey gang' as Sean likes to say. Let's take a look at our Thunder Client again and let's make that same request. Hit send. You should see that now it says our updated message.
Now, we didn't really go into detail about what modules, controllers, and services are. In the following videos we're going to break down each of these topics and more. We've got a full crash course for you here today, and we're going to build again a Net Ninja API. You're going to manage your own army of ninjas. So make sure to head on over to the next video where we'll talk about what are modules and in general creating new resources for our API.