Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It sits atop Node.js as a de facto standard server framework for REST APIs, web applications, and microservices. Express 5 (released October 2024) modernizes the framework by requiring Node.js 18+, automatically forwarding rejected promises to error handlers, tightening route-pattern security via path-to-regexp v8, and adding native Brotli compression support. Understanding middleware execution order, asynchronous error handling patterns, and the request-response lifecycle is essential for leveraging Express effectively in production.
What This Cheat Sheet Covers
This topic spans 28 focused tables and 197 indexed concepts, 156 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Application Methods
These are the methods you call on the app object itself — the ones that boot the server, mount middleware, and tune how the whole application behaves. app.use() and app.listen() show up in nearly every Express file, while settings like app.disable('x-powered-by') and app.enable('trust proxy') are the small touches that make an app production-ready.
| Method | Example | Description | |
|---|---|---|---|
app.listen(3000, () => console.log('Server running')) | Starts the Express application and binds it to a specified port, listening for incoming HTTP requests. | ||
app.use(express.json())app.use('/api', routes) | • Mounts middleware functions at a specified path • executes for every request matching that path, or globally if no path specified. | ||
app.set('view engine', 'ejs')app.set('port', 3000) | • Assigns a configuration setting to a name • used for application-level settings like template engine or custom properties. | ||
const port = app.get('port') | Retrieves the value of a setting previously assigned with app.set(). | ||
app.locals.title = 'My App' | Object containing application-level variables accessible throughout the app's lifecycle, including inside templates. | ||
app.enable('trust proxy') | • Sets a boolean setting to true• enables features like proxy trust for correct IP detection behind load balancers. | ||
app.disable('x-powered-by') | • Sets a boolean setting to false• commonly used to remove the X-Powered-By header for security. | ||
app.param('id', (req, res, next, id) => { next() }) | • Adds callback triggers for route parameters • executes middleware whenever a specific named parameter is present. | ||
const router = app.router | • Returns a reference to the base router instance (reintroduced in Express 5) • simplifies route management without explicit initialization. | ||
app.path() // '/admin' | • Returns the canonical path of the mounted sub-app or router • useful for debugging complex mount hierarchies. |