From c7c701ee417ac3e447f1897daa7fb48315409d9e Mon Sep 17 00:00:00 2001 From: dsbly1 Date: Tue, 16 Jun 2026 17:14:48 -0500 Subject: [PATCH 1/3] docs: add code examples for Router options (caseSensitive, mergeParams, strict) --- src/content/api/5x/api/express/index.mdx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/content/api/5x/api/express/index.mdx b/src/content/api/5x/api/express/index.mdx index eb191f96fd..0e9e4f17d3 100644 --- a/src/content/api/5x/api/express/index.mdx +++ b/src/content/api/5x/api/express/index.mdx @@ -267,6 +267,28 @@ Creates a new [router](/api/router/) object. const router = express.Router([options]); ``` +**Examples:** + +```js +// caseSensitive: treat "/Foo" and "/foo" as different routes +const router = express.Router({ caseSensitive: true }); +router.get('/Foo', (req, res) => res.send('Matched /Foo exactly')); + +// mergeParams: access parent route params in child router +const parent = express.Router(); +const child = express.Router({ mergeParams: true }); + +parent.use('/user/:userId', child); +child.get('/comments', (req, res) => { + // req.params.userId is available because mergeParams: true + res.send(`Comments for user ${req.params.userId}`); +}); + +// strict: treat "/foo" and "/foo/" as different routes +const strictRouter = express.Router({ strict: true }); +strictRouter.get('/foo/', (req, res) => res.send('Matched /foo/ with trailing slash')); +``` + You can add middleware and HTTP method routes (such as `get`, `put`, `post`, and so on) to `router` just like an application. From dbdf38a86bb71c741ba04fb44fa23ecc4bfc28c3 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sat, 24 Oct 2026 14:07:35 -0500 Subject: [PATCH 2/3] docs: embed Router option examples in 4.x and 5.x references --- src/content/api/4x/api/express/index.mdx | 28 +++++++++++++ src/content/api/5x/api/express/index.mdx | 50 +++++++++++++----------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/content/api/4x/api/express/index.mdx b/src/content/api/4x/api/express/index.mdx index 540a0ed176..3cfab46f32 100644 --- a/src/content/api/4x/api/express/index.mdx +++ b/src/content/api/4x/api/express/index.mdx @@ -241,15 +241,43 @@ app.post('/upload', (req: Request, res: Response) => { Enable case sensitivity. Disabled by default, treating `/Foo` and `/foo` as the same. + + ```js + // caseSensitive: treat "/Foo" and "/foo" as different routes + const router = express.Router({ caseSensitive: true }); + router.get('/Foo', (req, res) => res.send('Matched /Foo exactly')); + ``` + Preserve the `req.params` values from the parent router. If the parent and the child have conflicting param names, the child's value take precedence. + + ```js + // mergeParams: access parent route params in child router + const parent = express.Router(); + const child = express.Router({ mergeParams: true }); + + parent.use('/user/:userId', child); + child.get('/comments', (req, res) => { + // req.params.userId is available because mergeParams: true + res.send(`Comments for user ${req.params.userId}`); + }); + ``` + Enable strict routing. Disabled by default, `/foo` and `/foo/` are treated the same by the router. + + ```js + // strict: treat "/foo" and "/foo/" as different routes + const strictRouter = express.Router({ strict: true }); + strictRouter.get('/foo/', (req, res) => res.send('Matched /foo/ with trailing slash')); + ``` + + diff --git a/src/content/api/5x/api/express/index.mdx b/src/content/api/5x/api/express/index.mdx index 0e9e4f17d3..86e69f02c2 100644 --- a/src/content/api/5x/api/express/index.mdx +++ b/src/content/api/5x/api/express/index.mdx @@ -249,15 +249,43 @@ app.post('/upload', (req: Request, res: Response) => { Enable case sensitivity. Disabled by default, treating `/Foo` and `/foo` as the same. + + ```js + // caseSensitive: treat "/Foo" and "/foo" as different routes + const router = express.Router({ caseSensitive: true }); + router.get('/Foo', (req, res) => res.send('Matched /Foo exactly')); + ``` + Preserve the `req.params` values from the parent router. If the parent and the child have conflicting param names, the child's value take precedence. + + ```js + // mergeParams: access parent route params in child router + const parent = express.Router(); + const child = express.Router({ mergeParams: true }); + + parent.use('/user/:userId', child); + child.get('/comments', (req, res) => { + // req.params.userId is available because mergeParams: true + res.send(`Comments for user ${req.params.userId}`); + }); + ``` + Enable strict routing. Disabled by default, `/foo` and `/foo/` are treated the same by the router. + + ```js + // strict: treat "/foo" and "/foo/" as different routes + const strictRouter = express.Router({ strict: true }); + strictRouter.get('/foo/', (req, res) => res.send('Matched /foo/ with trailing slash')); + ``` + + @@ -267,28 +295,6 @@ Creates a new [router](/api/router/) object. const router = express.Router([options]); ``` -**Examples:** - -```js -// caseSensitive: treat "/Foo" and "/foo" as different routes -const router = express.Router({ caseSensitive: true }); -router.get('/Foo', (req, res) => res.send('Matched /Foo exactly')); - -// mergeParams: access parent route params in child router -const parent = express.Router(); -const child = express.Router({ mergeParams: true }); - -parent.use('/user/:userId', child); -child.get('/comments', (req, res) => { - // req.params.userId is available because mergeParams: true - res.send(`Comments for user ${req.params.userId}`); -}); - -// strict: treat "/foo" and "/foo/" as different routes -const strictRouter = express.Router({ strict: true }); -strictRouter.get('/foo/', (req, res) => res.send('Matched /foo/ with trailing slash')); -``` - You can add middleware and HTTP method routes (such as `get`, `put`, `post`, and so on) to `router` just like an application. From 7fca45571260991a09a97f15864a83062eb103f5 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sat, 24 Oct 2026 14:16:47 -0500 Subject: [PATCH 3/3] docs: add API option examples for Express 4 and 5 --- src/content/api/4x/api/express/index.mdx | 58 ++++++++++++++++++++++++ src/content/api/4x/api/request/index.mdx | 11 +++++ src/content/api/5x/api/express/index.mdx | 58 ++++++++++++++++++++++++ src/content/api/5x/api/request/index.mdx | 11 +++++ 4 files changed, 138 insertions(+) diff --git a/src/content/api/4x/api/express/index.mdx b/src/content/api/4x/api/express/index.mdx index 3cfab46f32..b4f5d0a794 100644 --- a/src/content/api/4x/api/express/index.mdx +++ b/src/content/api/4x/api/express/index.mdx @@ -47,10 +47,26 @@ const app: Express = express(); The `reviver` option is passed directly to `JSON.parse` as the second argument. You can find more information on this argument [in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter). + + ```js + app.use(express.json({ + reviver: (key, value) => key === 'createdAt' ? new Date(value) : value, + })); + // Body: {"createdAt":"2026-01-01T00:00:00Z"} + // req.body.createdAt is a Date. + ``` + Enables or disables only accepting arrays and objects; when disabled will accept anything `JSON.parse` accepts. + + ```js + app.use(express.json({ strict: false })); + // Body: 42 -> req.body is the number 42. + // With strict: true (the default), this body is rejected with status 400. + ``` + This is used to determine what media type the middleware will parse. This option can be a @@ -64,6 +80,16 @@ const app: Express = express(); This option, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. + + ```js + app.use(express.json({ + verify: (req, res, buf) => { + req.rawBody = buf; + }, + })); + // req.rawBody contains the original Buffer, before JSON parsing. + ``` + @@ -324,12 +350,32 @@ For more information, see [Router](/api/router/). Let client errors fall-through as unhandled requests, otherwise forward a client error. See [fallthrough](#fallthrough) below. + + ```js + app.use('/assets', express.static('public', { fallthrough: true })); + app.use('/assets', (req, res) => { + res.status(404).send('Asset not found'); + }); + // Missing files reach the fallback above. + // With fallthrough: false, they reach error-handling middleware instead. + ``` + Enable or disable the `immutable` directive in the `Cache-Control` response header. If enabled, the `maxAge` option should also be specified to enable caching. The `immutable` directive will prevent supported clients from making conditional requests during the life of the `maxAge` option to check if the file has changed. + + ```js + // Use filenames that change whenever the file contents change. + app.use('/assets', express.static('public', { + maxAge: '1y', + immutable: true, + })); + // Cache-Control: public, max-age=31536000, immutable + ``` + Sends the specified directory index file. Set to `false` to disable directory indexing. @@ -600,6 +646,18 @@ app.post('/', (req: Request, res: Response) => { rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please [see the qs library](https://www.npmjs.com/package/qs#readme). + + ```js + // Form body: user[name]=Tobi + app.post('/simple', express.urlencoded({ extended: false }), (req, res) => { + res.json(req.body); // => { 'user[name]': 'Tobi' } + }); + + app.post('/extended', express.urlencoded({ extended: true }), (req, res) => { + res.json(req.body); // => { user: { name: 'Tobi' } } + }); + ``` + Enables or disables handling deflated (compressed) bodies; when disabled, deflated bodies diff --git a/src/content/api/4x/api/request/index.mdx b/src/content/api/4x/api/request/index.mdx index ebd3ba575e..3194ce4450 100644 --- a/src/content/api/4x/api/request/index.mdx +++ b/src/content/api/4x/api/request/index.mdx @@ -983,6 +983,17 @@ Body-parsing middleware must be loaded for `req.param()` to work predictably. Re Specify if overlapping and adjacent ranges should be combined. When `true`, ranges are combined and returned as if they were specified that way in the header. + + ```js + // Range: bytes=0-99,50-149 + const separate = req.range(1000, { combine: false }); + console.log(separate[0]); // => { start: 0, end: 99 } + console.log(separate[1]); // => { start: 50, end: 149 } + + const combined = req.range(1000, { combine: true }); + console.log(combined[0]); // => { start: 0, end: 149 } + ``` + diff --git a/src/content/api/5x/api/express/index.mdx b/src/content/api/5x/api/express/index.mdx index 86e69f02c2..df72335acf 100644 --- a/src/content/api/5x/api/express/index.mdx +++ b/src/content/api/5x/api/express/index.mdx @@ -55,10 +55,26 @@ The Express object has the following methods that can be used to create middlewa The `reviver` option is passed directly to `JSON.parse` as the second argument. You can find more information on this argument [in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter). + + ```js + app.use(express.json({ + reviver: (key, value) => key === 'createdAt' ? new Date(value) : value, + })); + // Body: {"createdAt":"2026-01-01T00:00:00Z"} + // req.body.createdAt is a Date. + ``` + Enables or disables only accepting arrays and objects; when disabled will accept anything `JSON.parse` accepts. + + ```js + app.use(express.json({ strict: false })); + // Body: 42 -> req.body is the number 42. + // With strict: true (the default), this body is rejected with status 400. + ``` + This is used to determine what media type the middleware will parse. This option can be a @@ -72,6 +88,16 @@ The Express object has the following methods that can be used to create middlewa This option, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. + + ```js + app.use(express.json({ + verify: (req, res, buf) => { + req.rawBody = buf; + }, + })); + // req.rawBody contains the original Buffer, before JSON parsing. + ``` + @@ -332,12 +358,32 @@ For more information, see [Router](/api/router/). Let client errors fall-through as unhandled requests, otherwise forward a client error. See [fallthrough](#fallthrough) below. + + ```js + app.use('/assets', express.static('public', { fallthrough: true })); + app.use('/assets', (req, res) => { + res.status(404).send('Asset not found'); + }); + // Missing files reach the fallback above. + // With fallthrough: false, they reach error-handling middleware instead. + ``` + Enable or disable the `immutable` directive in the `Cache-Control` response header. If enabled, the `maxAge` option should also be specified to enable caching. The `immutable` directive will prevent supported clients from making conditional requests during the life of the `maxAge` option to check if the file has changed. + + ```js + // Use filenames that change whenever the file contents change. + app.use('/assets', express.static('public', { + maxAge: '1y', + immutable: true, + })); + // Cache-Control: public, max-age=31536000, immutable + ``` + Sends the specified directory index file. Set to `false` to disable directory indexing. @@ -607,6 +653,18 @@ app.post('/', (req: Request, res: Response) => { rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please [see the qs library](https://www.npmjs.com/package/qs#readme). + + ```js + // Form body: user[name]=Tobi + app.post('/simple', express.urlencoded({ extended: false }), (req, res) => { + res.json(req.body); // => { 'user[name]': 'Tobi' } + }); + + app.post('/extended', express.urlencoded({ extended: true }), (req, res) => { + res.json(req.body); // => { user: { name: 'Tobi' } } + }); + ``` + Enables or disables handling deflated (compressed) bodies; when disabled, deflated bodies diff --git a/src/content/api/5x/api/request/index.mdx b/src/content/api/5x/api/request/index.mdx index 39916cc7aa..27e19c359f 100644 --- a/src/content/api/5x/api/request/index.mdx +++ b/src/content/api/5x/api/request/index.mdx @@ -897,6 +897,17 @@ For more information, or if you have issues or concerns, see [type-is](https://g Specify if overlapping and adjacent ranges should be combined. When `true`, ranges are combined and returned as if they were specified that way in the header. + + ```js + // Range: bytes=0-99,50-149 + const separate = req.range(1000, { combine: false }); + console.log(separate[0]); // => { start: 0, end: 99 } + console.log(separate[1]); // => { start: 50, end: 149 } + + const combined = req.range(1000, { combine: true }); + console.log(combined[0]); // => { start: 0, end: 149 } + ``` +