Back to Blog
Lesson 17 of the Node.js: Build Your First Server & CLI course
Node.jsAugust 4, 20264 min read

Initializing Express.js: Build Your First Node.js Web Server

Learn how to install Express.js, initialize your first web server, and listen for requests on a specific port. Start building your REST API project today.

Node.jsExpress.jsBackendWeb DevelopmentAPI
Modern server rack with blue lighting in a secure data center environment.

Previously in this course, we explored the HTTP request-response cycle, where we learned how to build a server using Node.js's built-in http module. While that was essential for understanding the fundamentals, managing raw HTTP streams manually is tedious and error-prone as your application grows.

In this lesson, we’re leveling up. We will install Express.js, a lightweight framework that abstracts the complexities of the http module, allowing us to build a robust web server with minimal boilerplate. This is the first step in our journey to initializing the REST API for our Task Management project.

Why Express.js?

Node.js provides a http module, but it lacks built-in features for routing, middleware management, or easy JSON parsing. Express.js is the industry-standard framework that wraps this functionality into a clean, developer-friendly API. It allows us to handle incoming requests through a "middleware" pipeline, which we'll explore in later lessons.

Installing Express

A worker using a drill to install window blinds in an indoor setting.

Before we write code, we need to add the Express library to our project dependencies. Since you’ve already mastered managing dependencies with npm, the process is straightforward.

Open your terminal in your project directory and run:

Bash
npm install express

This command downloads the package and adds it to your package.json file under dependencies.

Initializing an Express App

Once installed, we need to import Express and instantiate our application. In your project root, create a file named index.js.

The structure of an Express app is simple: you create an instance of the express function, which exposes methods to define routes and start the server.

JAVASCRIPT
// index.js
const express = require(CE9178">'express');
const app = express();

// Define a basic route
app.get(CE9178">'/', (req, res) => {
  res.send(CE9178">'Hello, World! Our server is running.');
});

Here, app is the central object that manages the application's configuration, settings, and routing table.

Starting the Server on a Specific Port

To make our server accessible, we must tell it to "listen" for incoming traffic on a specific port. We use the app.listen() method for this.

JAVASCRIPT
const PORT = 3000;

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

When you run node index.js, the process stays alive because listen keeps the event loop active, waiting for network events on port 3000.

The Setup Workflow

StepCommand/ActionPurpose
1npm init -yInitialize the manifest file.
2npm install expressAdd the framework to node_modules.
3const app = express()Create the application instance.
4app.listen(3000)Bind the server to a local network port.

Hands-on Exercise

  1. Create a folder named task-api and navigate into it via your terminal.
  2. Initialize the project with npm init -y.
  3. Install express.
  4. Create an index.js file and implement the server code shown above.
  5. Run the server using node index.js.
  6. Open your browser and navigate to http://localhost:3000 to see your response.

Common Pitfalls

  • Port Conflicts: If you get an EADDRINUSE error, it means another process is already using port 3000. Either stop that process or change the PORT variable to something else, like 3001.
  • Forgetting to Import: Express must be imported using require('express') (or import if using ESM). The express variable itself is a function that returns the app object; you cannot call express() if you only imported the module incorrectly.
  • Missing the Listener: If your script runs and exits immediately, you likely forgot to call app.listen(). A web server must keep the process alive to accept incoming requests.

FAQ

Q: Can I use multiple ports? A: You can only have one server listening on a specific port per IP address. However, you can create multiple app instances and have them listen on different ports if needed.

Q: Is Express the only framework for Node.js? A: No, there are others like Fastify or Koa, but Express remains the most widely used and best documented for beginners.

Q: Do I need to restart the server every time I change code? A: Yes, for now. Every time you save index.js, you must stop the process (Ctrl+C) and run node index.js again to see changes. We will introduce tools to automate this later.

Recap

In this lesson, we transitioned from using the low-level http module to the robust Express.js framework. You learned how to install dependencies via npm, instantiate an Express application, and start an HTTP server that listens on a specific port. These are the foundational blocks for every REST API we will build moving forward.

Up next: Routing in Express, where we will learn how to handle different URLs and define specific endpoints for our API.

Similar Posts