From the course: Learning Koa

Install and set up Koa

From the course: Learning Koa

Start my 1-month free trial

Install and set up Koa

- [Instructor] Let's go ahead and set up our initial Koa server. So, the first thing we're going to do is install the few dependencies that we need inside of our server and I'm going to go ahead and do npm install and you can do i if you want. Koa and then nodemon as well. So, this is the reason why I told you to change the actual name of the package here because if you wanted to install Koa and your package was named Koa, you would have a problem right there. So, let's go ahead and install those. Okay, perfect. So, once you see those two dependencies installed inside of your package here, you're good to go. The next thing we're going to do is insert a script inside of our package.json file here. So, when we do npm start, want to make sure that the right script is run inside of our scripts here, so let's go ahead and do that. And we'll change this to start and then the script, we're going to do nodemon index.js. So, what's going to happen once we do npm start, it's going to run this script here and it's going to use nodemon to run index.js and what's going to happen is whenever we make changes to index.js or the server, it will restart automatically because of nodemon. So, let's go ahead and create our index file now and add the code that we need inside of that file. So, we're going to call this one index.js and let's make sure that we save our package.json file here, so save this, then we can close it. Let me shut this down for now while we're doing the code here. And the first thing you'll need to do is use Koa, so we're going to do const, I'm going to do capital K and we're going to require koa like so. Then we're going to create another variable which we'll call app which will be our main server and create a new server with Koa and run that function and then what we're going to do is create some code that we'll explore in the next few videos, so I'm not going to explain everything that I'm doing now, we'll explore it in the next few videos. So, I'm going to explain just briefly what I'm doing, so right now I'm leveraging the app that I just created, so the Koa app and I'm going to use the method use and then we're going to use the context and I'll explain what that is later on. And use the context again and pass inside of our body a message that we'll see on our server, and we'll say Hello Manny. And then we're going to do app.listen and pass the actual port that we need to listen on. So, if you've done any Express in the past, a lot of this will make sense and a lot of this also looks familiar because we have a listen, we have a use, we're using app which is a variable that we're passing the new Koa and this is very similar to Express but once we start looking into the details of Koa, this is where you're going to start seeing the differences in between Express and Koa. So, let's save this file, let's go back to our terminal, so click on View, Terminal and now we're going to run the script that we just did, so start, so we're going to do npm start and once you see this, that means everything is working as it should and let's bring up a new window, localhost:3000 and you see the response Hello Manny and this is the response that we're expecting. So, what we're going to do in the next few videos is explore the syntax that we just went through together.

Contents