Loading
Lesson 03
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Setting Up and Running the Express Server

Video Transcript

So let's continue. We're going to create a file called app.js. That's going to hold our startup code for the server. So I'm going to go here under our projects directory. I am going to create the file app.js. Okay, so let's get started writing our first Express server. First, you're going to need to import or require Express. So let's do it. We're going to do a const variable. Let's call it express. And we're going to do a require with the express package name. Okay, so after requiring Express, you got this guy right here. We're going to use this guy and call it as though it were a function. So we're going to do the following like this. Okay, so when you call this guy, you're going to get an express application. So we're going to start this in a variable called app. Okay, now our app needs to run on a specific port. Let's create a variable for the port. We can use any port you want. Let's do 3000. Now, to make our app listen to the outside world, we have to tell it to listen. So we're going to call app.listen. We need to give it a port. This gives 3000, but we have a variable. So we're going to say port. And then you pass a callback that you want to be executed after it started listening. In this case, we just want to display a message to the console saying that the server is now listening to any requests. So we're going to do console log. Okay. Now we're going to say express server is now listening to incoming connections or whatever. Let's say just listening. Let's also say the port, but that's useful for the person who starts up the server because you need to know what part to access the server at. So we can use either string concatenation here or string interpolation. Let's have about leaves interpolation. So since ES6, we can use interpolation if we substitute the quotes, single quotes here in my case, with a back tick. Back tick is the key next to the latter one in the American keyboard. Next to the number one, sorry. Okay. So express service now listening on. So I'm going to use a dollar sign with the curly braces to interpolate the value. The variable port here has the value 3000 that we will be substituted here in this part. Okay. Think of it that way. Anyway, so let me enable soft wrap so you can see the line breaks there. I can decrease the font size. So let's review what we wrote. First, we have to require the express. Okay. Package module. Then you call on that module to get the application, the express application. We define a port variable to hold just to hold the value of the part at which the server will be running. Then we tell our app to listen at the part 3000 in this case. And after that we just say console log to tell the user who powered up the server that the server is now listening on the specified part. This case 3000. Okay. Let's get this running. Let's go to the terminal. And we're going to, you can see the files are here. You have app.js. So you're just simply going to say node app.js. Now it's at the message. Express servers allow listening on 3000. Oops. I should say the word to port by the way on port 3000. Let's try again. Just to make it nicer. Oh, but before I try again, how do I get out of this? Right. So I can use control C to kill the server control C. Okay. And that will send a signal to stop the server. I can try again. Now I added the word port there. Now it's nice. Now it's listening. Okay. So express servers listening a port 3000. So you can access this guy at local host H2P colon slash slash local host colon 3000. But right now we don't have any routes. So I don't know what we're going to get. Let's try it out.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: