Express Intro

View saved

Install Express

In a project with package.json:

npm install express

Minimal app

Save as app.js (ESM example).

import express from "express";

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

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

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

Route parameters

Capture dynamic path segments.

app.get("/users/:id", (req, res) => {
  res.send(`User ${req.params.id}`);
});

Send status codes

Use helpers when something is missing or forbidden.

res.status(404).send("Not found");

Comments

One comment per signed-in account. Comments are saved with this page’s URL.