Skip to main content
Express.js Tutorial
CHAPTER 04 Beginner

Creating Your First Express Server

Updated: May 14, 2026
20 min read

# CHAPTER 4

Creating Your First Express Server

1. Introduction

The foundation of any backend application is the server itself. It is the program that sits in memory, listening to a specific network port, waiting for incoming internet traffic. In this chapter, we will write the foundational code required to instantiate an Express application, define a basic route, and turn the server "on."

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Import and instantiate the Express module.
  • Understand the app object.
  • Define a basic GET route.
  • Use app.listen() to start the server on a specific port.

3. Beginner-Friendly Explanation

Imagine opening a physical store.
  1. 1. Importing Express: This is legally registering your business name.
  1. 2. Instantiating app: This is signing the lease for the building. You now have an empty store (const app = express();).
  1. 3. Defining a Route: This is placing a cashier at the front desk and giving them a script: "When someone walks in and says 'Hello', you reply with 'Welcome'." (app.get(...))
  1. 4. Listening on a Port: This is unlocking the front doors and flipping the "Open" sign so customers can actually enter. (app.listen(3000))

4. Step 1: The Code Skeleton

Ensure you have initialized your project and installed Express (npm install express), as covered in Chapter 2. Create a file in your project root named index.js.
javascript
12345678
// 1. Import the Express framework
const express = require('express');

// 2. Instantiate the application (Creates the 'app' object)
const app = express();

// 3. Define the Port number the server will listen on
const PORT = 3000;

5. Step 2: Defining a Basic Route

The app object contains methods for HTTP verbs (get, post, put, delete). We will define a simple GET route for the homepage (/).
javascript
1234567
// When a GET request hits the root URL ('/'), execute this callback function
app.get('/', (req, res) => {
    
    // Send plain text back to the browser
    res.send('Welcome to my first Express Server!');
    
});

*Note: req represents the Request, and res represents the Response.*

6. Step 3: Starting the Server

Right now, the code exists, but it is not listening for traffic. We must use the listen() method to bind the application to a port on our computer.
javascript
1234
// Bind the app to Port 3000 and run a callback when successful
app.listen(PORT, () => {
    console.log(`Server is successfully running on http://localhost:${PORT}`);
});

7. The Complete File

Here is your complete index.js file:
javascript
1234567891011
const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
    res.send('Welcome to my first Express Server!');
});

app.listen(PORT, () => {
    console.log(`Server is successfully running on http://localhost:${PORT}`);
});

8. Running the Server

Open your terminal, ensure you are in the project folder, and run:
bash
1
node index.js

You will see Server is successfully running on http://localhost:3000 printed in the terminal. Open your web browser (Google Chrome) and type http://localhost:3000 into the address bar. You will see your welcome message!

To stop the server, go to your terminal and press Ctrl + C.

9. Best Practices

  • Use Environment Variables for Ports: In professional applications, you should never hardcode const PORT = 3000;. When you deploy to a cloud provider like Heroku or AWS, the cloud server assigns a random port dynamically. You should write:
const PORT = process.env.PORT || 3000; *(This means: Use the cloud's port, but if it doesn't exist because we are on my local laptop, use 3000).*

10. Common Mistakes

  • The "Address Already in Use" Error: If you run node index.js and get an EADDRINUSE: address already in use :::3000 error, it means you already have a Node server running in the background (or in another terminal window) that is occupying Port 3000. Stop the other server, or change this script's port to 3001.

11. Exercises

  1. 1. Trace the flow: You type http://localhost:3000 into Chrome and hit Enter. What exact code block in index.js catches this action, and how does it respond?

12. Coding Challenges

  • Challenge: Add a second route to your index.js file. If the user visits http://localhost:3000/api/status, the server should use res.send() to reply with the text: "API is online and healthy". Run the server and test it in your browser.

13. MCQs with Answers

Question 1

What is the purpose of the app.listen() method in Express?

Question 2

When writing const PORT = process.env.PORT || 3000;, what does this code achieve?

14. Interview Questions

  • Q: Explain the role of the req and res objects passed into an Express route callback function.
  • Q: Why do developers use nodemon instead of the standard node command during development? What problem does it solve?

15. FAQs

Q: Can I use res.send() multiple times in one route? A: No! You can only send *one* response per request. If you write res.send('A'); res.send('B');, Express will crash with a "Cannot set headers after they are sent to the client" error.

16. Summary

In Chapter 4, we brought our application to life. We imported the Express module, instantiated the core app object, and defined our very first HTTP GET route. By utilizing the app.listen() method, we bound our application to a network port, allowing it to successfully intercept web traffic from a browser and return a custom text response.

17. Next Chapter Recommendation

Our server is running, but it only knows one URL (/). Real applications have hundreds of URLs. Proceed to Chapter 5: Express Routing Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·