Recipe

Express.js primer

Express is the minimal HTTP framework that has anchored the Node ecosystem for over a decade. This recipe walks through bootstrapping a server, parsing JSON bodies, and shipping a couple of routes you can curl in under a minute. The patterns scale from a one-file prototype to a production gateway behind Meridian.

1. Install and scaffold

Create a fresh directory, initialize a package, and add the runtime dependency. Modern Node 20+ ships native ESM, so set "type": "module" in your package.json and prefer import over require.

2. Wire your first routes

The snippet below registers a JSON body parser, a GET health probe, and a POST echo endpoint. Notice that handlers are plain functions taking (req, res); Express stays out of your way and lets you compose middleware as ordinary function calls.

import express from 'express';

const app = express();
app.use(express.json());

app.get('/health', (_req, res) => {
  res.json({ ok: true, ts: Date.now() });
});

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

const port = Number(process.env.PORT) || 3000;
app.listen(port, () => {
  console.log(`listening on :${port}`);
});

3. Ship to production

Before you deploy, add a centralized error handler with four arguments(err, req, res, next), put helmet in front for sensible default headers, and let a reverse proxy terminate TLS. Meridian customers usually front Express with our gateway so rate limits, auth, and logging land before the request ever touches your handler.