Introduction to Express and Quick Start

Learn what Express is, why it is useful, and how to set up a project and run your first Express server.

🎯 Introduction

In the previous course, we built a server with the native Node.js http module. It worked, but required quite a bit of boilerplate. By the end of this article, you will understand what Express is and which problems it solves, and be able to set up a project and run your first Express server from scratch.


🧱 What Is Express?

Express is a web framework built on the Node.js http module, and a popular backend framework in the Node.js ecosystem. It does not introduce new underlying capabilities. Instead, it wraps common server tasks in simple APIs:

  • Routing: No need to write a series of if/else checks against req.url.
  • Request handling: Ready-made tools for parsing JSON request bodies and query parameters.
  • Responses: Return JSON in one line without setting Content-Type manually.
  • Middleware: Plug in shared functionality such as logging, authentication, and static file serving like building blocks.

Think of the http module as engine parts and Express as an assembled car. You can learn to drive without first learning to manufacture the parts.


✨ Why Use Express?

Recall how we defined routes with the native http module in the previous course:

server.js
import http from 'node:http';

const server = http.createServer((req, res) => {
    if (req.url === '/' && req.method === 'GET') {
        res.setHeader('Content-Type', 'text/plain; charset=utf-8');
        res.end('Welcome to the home page');
    } else {
        res.statusCode = 404;
        res.end('Page not found');
    }
});

server.listen(3000);

Here is the same functionality in Express:

app.js
import express from 'express';

const app = express();

app.get('/', (req, res) => {
    res.send('Welcome to the home page');
});

app.listen(3000);

The framework handles route matching and response headers, cutting the code roughly in half and making it easier to read. The more endpoints you add, the more this helps—that is the value of a framework.


🛠 Set Up a Project

The examples in this article and course were written and tested with Node.js v22 LTS and Express 5.2.1. Express 5 is a major version released in 2024. Some syntax differs from older Express 4 tutorials online, so pay attention to the version you are following.

Create a project directory, open it in your terminal, and run:

npm init -y
npm install express

The first command creates package.json; the second installs Express. Next, add "type": "module" to package.json to enable ES modules, as in the previous Node.js course:

package.json
{
    "name": "express-demo",
    "type": "module"
}
Without "type": "module", the import syntax can cause an error. Older tutorials often use require. This course consistently uses ESM, with the same syntax you use in frontend Vite projects.

🛠 Run Your First Server

Create app.js and add the following code:

app.js
import express from 'express';

// Create an application instance
const app = express();

// Define a route: handle GET requests to /
app.get('/', (req, res) => {
    res.send('Hello Express');
});

// Listen on port 3000
app.listen(3000, () => {
    console.log('Server running at http://localhost:3000');
});

Run node app.js in your terminal, then open http://localhost:3000 in a browser. You should see Hello Express. There are just three key steps: create the application, define a route, and listen on a port.


🛠 Develop with Watch Mode

Pressing Ctrl+C and running node app.js again after every change quickly becomes tedious. Node.js has a built-in watch mode that restarts the process when you save a file:

node --watch app.js

Use this command during development. After changing the code, refresh the browser to see the result without restarting the server manually.


❓ Questions and Answers

Q1: How are Express and Node.js related?

Node.js is the runtime, and Express is a framework package that runs in it. Under the hood, Express still uses the Node.js http module, with a more convenient API on top.

Q2: Was learning the http module in the previous course a waste of time?

No. Express routing and responses wrap the capabilities of the http module. Understanding those foundations helps you know where to look when troubleshooting. Also, some projects do not use a framework.

Q3: Are Express 4 and Express 5 very different?

The core features—routing, middleware, requests, and responses—are largely the same. The main differences involve a few areas such as wildcard route syntax and asynchronous error handling. Follow the Express 5 examples in this course, and check the version when reading older tutorials.

Q4: What if port 3000 is already in use?

Change listen(3000) to an available port, such as 3001, or find and stop the program currently using port 3000.


🧪 Exercise

Add an /about route to your project that returns “About us”.

app.js
import express from 'express';

const app = express();

app.get('/', (req, res) => {
    res.send('Hello Express');
});

// Write your code here

app.listen(3000);

🎉 You have your first Express server running! In the next article, we will explore routing, including request methods such as GET and POST, and route parameters.