In this video, Emmanuel Henri starts coding the express server and adding the proper scripts to run your server on port 3000. He then tests the server to make sure everything is working properly.
- [Instructor] Now the fun begins. We'll start by creating our initial server code, and then making sure it is running properly. We'll simply work on the basis of our server which we'll add more and more as we build our back-end APIs. So the first thing I want you to install is body-parser so let's go ahead and add that dependency. So npm, let's go inside of vs code, and do npm i save body-parser, and we're saving this to the regular dependency.
Okay so once we have that installed we'll create a brand new file. So, as you remember, if we look in the package.json file the entry point of our server is the index.js. So that means when we do npm start it's going to look inside of that file, and see if there's a server, and if there is one then it will run it. So let's go ahead, and create the file so you do either File, New File, or you do the little icon here in VS Code or whichever way you create a new file in your own editor.
And we'll call it index.js, and we're simply going to create a very, very simple server first. So let's go ahead and do that so first we need to import express since we're going to create a web server. From express, and then we'll create a variable called app, and basically run an instance of express, and I want to create a variable for the port which we'll use over, and over, and over inside of our code, and the one good benefit of doing a port inside of a variable is that once you need to change the port you only need to change it at one place like so.
So let's go ahead and make sure that we get something when we are calling that specific URL in our server. And that will give us a response so we need to basically load the variable's request, and response, and then, inside of it we'll respond with sending a message.
Node and express server is running on port, and usually what I would do because this is a simple string that we're sending as a message I would do something like this. But, remember, we did our port in a variable so we don't want to have to repeat that number every single time we need to express what the port is, or use it inside of our applications. So what we'll do instead of doing a regular string we'll do, basically, template strings which is ES6 syntax.
So let's remove these single quotes, and replace it by grave. This is the character at the very top of your keyboard on the left of number one. And now with these characters, and with this curly braces here you can now load the actual port like so. And let's make sure we close our curly braces. So now whenever we send that message to our server we'll have the right port that we created inside of our variable.
Okay? So let's go ahead and close that out, and then we need to, basically, listen on the specific port so guess what? We'll use, again, that variable that we created. Port, and then run a function which will basically, let me just remove that message here, run a console log, and then use again the template strings.
Your server, it looks like I need a server, is running on port, and again, dollar sign, curly braces, and then you can load the variable here. So you could load anything inside of a template strings if you have a variable, and then use the dollar sign with the curly braces like so. Okay? So let's close that out. Let's make sure we didn't make any typos.
We're all good here. We're all good there. Let's save this, and now let's run our super server. So let's do an npm start, but I'm going to stop for a second. If I do an npm start nothing's going to happen because there's no script related to the start command here. If we look in the package.json here we have a script for test. So if I do npm test it's going to run this specific test script here. So we need to create the actual script for the start command.
So let's go ahead, and do that. And do start. So if we run npm start this is what we're going to do. So we're going to leverage nodemon to make sure that our server always refreshes when we change the code, and then we want to make sure that it runs the index.js using babel, and js.
So, with that said, we should be able to save, and then run npm start with no issues. So right now our server is running on port 3,000. Let's go inside of our browser, and do localhost, port 3,000, and the message we get which is the response from the server node and express server is running on port 3,000. So if you take a look at the code here, the inside of our index.js, this is the response that we're expecting.
Okay so now we got a server running ready to add more stuff to it so let's move on.
- Setting up a project and a server
- Setting up a database and schema
- Creating POST, GET, PUT, and DELETE endpoints
- Serving files