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.

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

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:
Bashnpm 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.
JAVASCRIPTconst 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
| Step | Command/Action | Purpose |
|---|---|---|
| 1 | npm init -y | Initialize the manifest file. |
| 2 | npm install express | Add the framework to node_modules. |
| 3 | const app = express() | Create the application instance. |
| 4 | app.listen(3000) | Bind the server to a local network port. |
Hands-on Exercise
- Create a folder named
task-apiand navigate into it via your terminal. - Initialize the project with
npm init -y. - Install
express. - Create an
index.jsfile and implement the server code shown above. - Run the server using
node index.js. - Open your browser and navigate to
http://localhost:3000to see your response.
Common Pitfalls
- Port Conflicts: If you get an
EADDRINUSEerror, it means another process is already using port 3000. Either stop that process or change thePORTvariable to something else, like3001. - Forgetting to Import: Express must be imported using
require('express')(orimportif using ESM). Theexpressvariable itself is a function that returns the app object; you cannot callexpress()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.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.
