Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 43 ยท Getting Started

Your First Express App

Create your first Express 5 app: install with npm, write a hello-world route, start it with app.listen, restart on changes, and try the same flow on a mini Express.

In this lesson you build the smallest useful Express server: it installs the package, creates an app, answers one route and listens on a port. You will also learn how to restart the server automatically while you edit code.

Because npm is not available in the in-browser runner, the real commands and code are read-only below. Right after each step you can run the same idea on the mini Express stand-in, so you see the request and response happen.

Step 1: create a project and install Express

You need Node.js 18 or newer. In an empty folder, run these commands in a terminal:

Terminal
mkdir hello-express
cd hello-express
npm init -y
npm install express

npm init -y writes a package.json with default values. npm install express downloads Express into node_modules/ and records it under dependencies. Since 2025 this installs Express 5. If you ever need to be explicit, npm install express@5 pins the major version.

Open package.json and add "type": "module" so that you can use import instead of require. Details are in Project Setup and Structure and in the package.json lesson.

JSON
{
  "name": "hello-express",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "express": "^5.1.0"
  }
}

Step 2: write the app

Create index.js:

JavaScript
import express from 'express';

const app = express();
const port = 3000;

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

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

Four things happen here:

  1. express() creates an application object. It is also a function that Node can call for every request.
  2. app.get('/', handler) registers a route: for a GET request to /, call the handler.
  3. res.send(...) writes the response: status 200, a content type picked from the value, and the body.
  4. app.listen(port, callback) starts a Node HTTP server and calls the callback when it is ready.

In Express 5 the listen callback receives an error argument if the server fails to start, for example when the port is already in use. In Express 4 such errors were thrown as an event and easy to miss. A production app should handle it: app.listen(port, (error) => { if (error) throw error; ... }).

Step 3: run it

Terminal
node index.js

Visit http://localhost:3000 in the browser or run curl http://localhost:3000. You should see Hello from Express!. Any other path, such as /nothing, shows Express's default 404 page: Cannot GET /nothing.

Stop the server with Ctrl+C.

Restarting on changes

Node does not reload your code by itself. Since Node 18.11 you can ask it to restart when files change:

Terminal
node --watch index.js

Many projects put this into package.json scripts so everyone uses the same command:

JSON
{
  "scripts": {
    "start": "node index.js",
    "dev": "node --watch index.js"
  }
}

Run it with npm run dev. Older tutorials use the nodemon package for the same job (npx nodemon index.js). It has more options, but --watch is enough for most small projects. Use npm start (without --watch) in production.

The same flow on the stand-in

The stand-in below follows the same steps: create an app, register a route, listen, then call it. Instead of a browser we use fetch to call our own server on a free port (0 means "pick any free port"), because that is the only way to make a request inside the runner.

Node.js Hello world on the mini Express

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The output shows two lines: the greeting with status 200 and the 404 text. The framework part is the same shape as the real one; only the code between the markers is ours.

More than one route

Real apps have many routes. Each one is just another app.get (or post, put, delete) call, and routes are checked in the order they were registered.

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

app.get('/health', (req, res) => {
  res.json({ status: 'ok' });
});

app.post('/echo', (req, res) => {
  res.json({ received: req.body });
});

The /echo route needs a body parser (express.json()) before req.body has any value; in Express 5 it is undefined otherwise. You will meet that in Built-in Middleware.

Node.js Several routes, one app

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The last call, GET /echo, gets a 404 because only POST /echo exists. Express matches the method and the path together.

Common first mistakes

  • Port already in use. Another program owns port 3000. Pick another port or stop the other process. Reading the port from an environment variable (process.env.PORT ?? 3000) is a common habit; see Secrets and Config.
  • Forgetting to send a response. If a handler neither calls res.send/res.json/res.end nor passes control on, the browser waits until it times out.
  • Sending twice. Calling res.send twice throws "Cannot set headers after they are sent". Use return res.send(...) when you reply early.
  • Using require in an ES module. With "type": "module" use import. See ES modules.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command installs Express into your project?

    npm install express downloads the package into node_modules and records it in package.json.
  2. 2. What does app.listen(3000) do?

    app.listen creates a Node HTTP server that uses the app to handle requests and starts accepting connections.
  3. 3. What restarts a Node program when its files change, without extra packages?

    The --watch flag, available since Node 18.11, restarts the process when imported files change.
  4. 4. You only registered app.post('/echo', ...). What does GET /echo return?

    Routes match both method and path. No GET route exists for /echo, so Express falls through to its default 404.

Summary

  • Install with npm install express, create an app with express(), register routes, then call app.listen.
  • res.send and res.json finish the response; send exactly one per request.
  • Use node --watch (or nodemon) while developing.
  • Routes match on method and path; unmatched requests get a 404.
  • The runnable stand-in uses fetch against a free port to show the same flow.

Next: Inside Express: Building a Mini Express.