Express.js
raw JSON →Express.js is a foundational, unopinionated web application framework for Node.js, widely regarded as the de facto standard for building web applications and APIs. It provides a robust yet minimalist set of features, including a powerful routing system, HTTP utility methods, and a flexible middleware architecture. The current stable major version is Express 5.x, with Express 5.1.0 being the default on npm, and an official LTS schedule supports both v4 and v5 release lines. The release cadence for major versions can be extended, as seen with the significant time between Express 4 and Express 5, which focused on improving core stability, aligning with modern Node.js features, and incorporating security enhancements rather than introducing a vast array of new features. Its key differentiators include its lightweight nature, high extensibility through a vast ecosystem of middleware, and its "unopinionated" approach, allowing developers significant freedom in structuring their applications and choosing components like ORMs and template engines. It is often a core component of the "MEAN" or "MERN" stack for full-stack JavaScript development. Note: The package 'expresss' is a likely typo; this entry refers to the correct package 'express'.
Common errors
error TypeError: app.del is not a function ↓
app.del('/path', handler) with app.delete('/path', handler). error TypeError: Cannot read properties of undefined (reading 'body') or req.body is undefined ↓
app.use(express.json()); and/or app.use(express.urlencoded({ extended: true })); (if parsing URL-encoded data) before your routes that access req.body. error ERR_REQUIRE_ESM or TypeError: require is not a function (when using import) ↓
"type": "module" in package.json or .mjs files), use import statements. If sticking to CommonJS, use require(). For specific cases needing both, consider dynamic import() or createRequire in Node.js. Warnings
breaking Express 5.x requires Node.js version 18 or higher. Applications running on older Node.js versions must be upgraded before migrating to Express 5. ↓
breaking Several deprecated method signatures from Express 4 have been removed in Express 5, including `app.del()`, `res.send(status, body)`, `res.send(status)`, `res.redirect('back')`, and `res.json(obj, status)`. ↓
breaking The path route matching syntax has stricter rules in Express 5, particularly concerning named wildcards (`*`) and regular expressions. For instance, `/*` should now be `/*splat`. ↓
breaking Body parsing behavior has changed in Express 5. `req.body` is no longer initialized to an empty object by default, and the `urlencoded` middleware's `extended` option now defaults to `false`. The standalone `bodyParser()` middleware is also removed. ↓
gotcha In Express 5, rejected promises returned from asynchronous middleware and route handlers are now automatically caught and forwarded to the error-handling middleware. This simplifies async error handling but might alter behavior for applications that previously handled rejections manually. ↓
Install
npm install expresss yarn add expresss pnpm add expresss Imports
- express wrong
const express = require('express');correctimport express from 'express'; - express wrong
import express from 'express';correctconst express = require('express'); - Router wrong
import Router from 'express/lib/router';correctimport { Router } from 'express';
Quickstart
import express from 'express';
const app = express();
const port = process.env.PORT ?? 3000;
app.get('/', (req, res) => {
res.send('Hello from Express.js!');
});
app.get('/api/data', (req, res) => {
res.json({ message: 'This is some API data!', timestamp: new Date() });
});
app.listen(port, () => {
console.log(`Express app listening on port ${port}`);
console.log(`Access at http://localhost:${port}`);
});