contentintech
Cheatsheets/Express
Backend

Express

Route requests, parse JSON and handle errors in Express 5.

Application

import express from 'express';Import Express in an ES module
const app = express();Create an application
app.use(express.json({ limit: '1mb' }));Parse JSON with a bounded body size
app.listen(3000);Start an HTTP listener

Routing

app.get('/health', (req, res) => res.json({ ok: true }));Send a JSON response
req.params.idRead a named route parameter
req.query.pageRead query input; validate its type before use
res.status(201).json({ id: 1 });Set status and send JSON

Middleware and errors

app.use('/api', router);Mount a router at a path
res.set('Cache-Control', 'no-store');Prevent caching a response
next(error);Pass an error to error-handling middleware
app.use((err, req, res, next) => res.status(500).json({ error: 'Unexpected error' }));Register four-argument error middleware after routes

Section navigation