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

Summary

## Creating a Basic Express Server

### Step 1: Create the Server File
1. Create a file named `app.js` in your project directory to hold the server's startup code.

### Step 2: Import Express
2. In `app.js`, import Express using the following code:
   ```javascript
   const express = require('express');

Step 3: Initialize the Express Application

  1. Call Express as a function to create an Express application:
    const app = express();
    

Step 4: Define Port

  1. Create a variable to hold the port number where the app will run:
    const port = 3000; // You can choose any port
    

Step 5: Make the App Listen

  1. Set the app to listen on the defined port and log a message to the console upon starting:
    app.listen(port, () => {
        console.log(`Express server is now listening on port ${port}`);
    });
    

Step 6: Run the Server

  1. Open the terminal and navigate to where app.js is located. Run the following command:
    node app.js
    
    • You should see a message indicating the server is listening on port 3000.

Step 7: Test the Server

  1. Access the server in your browser at:
    http://localhost:3000
    
    • Note: Currently, there are no routes defined, so you won't see any specific response.

Additional Notes

  • To stop the server, press Control + C in the terminal.
  • Ensure that you properly interpolate variables when logging messages for clarity.

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: