From b6c6192977735f488711ee82635f8084c961959c Mon Sep 17 00:00:00 2001 From: kushal4345 Date: Mon, 11 Aug 2025 10:41:37 +0530 Subject: [PATCH 1/6] docs(express.json): add example for route-specific limits (#1421) --- _includes/api/en/4x/express.json.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/_includes/api/en/4x/express.json.md b/_includes/api/en/4x/express.json.md index ffc460193d..1125284481 100644 --- a/_includes/api/en/4x/express.json.md +++ b/_includes/api/en/4x/express.json.md @@ -39,4 +39,13 @@ The following table describes the properties of the optional `options` object. | `type` | This is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, `type` option is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library and this can be an extension name (like `json`), a mime type (like `application/json`), or a mime type with a wildcard (like `*/*` or `*/json`). If a function, the `type` option is called as `fn(req)` and the request is parsed if it returns a truthy value. | Mixed | `"application/json"` | | `verify` | 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. | Function | `undefined` | - \ No newline at end of file + + +#### Example: Different limits for specific routes + +```js +// Limit 5MB for /upload +app.use('/upload', express.json({ limit: '5mb' })); + +// Default limit for other routes +app.use(express.json()); \ No newline at end of file From 0e581e146046af36d9e197f762a05d3e65b4da7a Mon Sep 17 00:00:00 2001 From: kushal4345 Date: Mon, 11 Aug 2025 10:47:51 +0530 Subject: [PATCH 2/6] fix(docs): remove extra semicolons in express.json.md --- _includes/api/en/4x/express.json.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_includes/api/en/4x/express.json.md b/_includes/api/en/4x/express.json.md index 1125284481..ef969b68c5 100644 --- a/_includes/api/en/4x/express.json.md +++ b/_includes/api/en/4x/express.json.md @@ -45,7 +45,7 @@ The following table describes the properties of the optional `options` object. ```js // Limit 5MB for /upload -app.use('/upload', express.json({ limit: '5mb' })); +app.use('/upload', express.json({ limit: '5mb' })) // Default limit for other routes -app.use(express.json()); \ No newline at end of file +app.use(express.json()) \ No newline at end of file From 7940a18e893a188e32df410950973cbc222db8a6 Mon Sep 17 00:00:00 2001 From: kushal4345 <155578739+kushal4345@users.noreply.github.com> Date: Mon, 18 Aug 2025 19:36:22 +0530 Subject: [PATCH 3/6] docs: include express.json example in using middleware page --- _includes/api/en/4x/express.json.md | 9 --------- _includes/api/en/4x/express.static.md | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/_includes/api/en/4x/express.json.md b/_includes/api/en/4x/express.json.md index ef969b68c5..70f0b7cbe2 100644 --- a/_includes/api/en/4x/express.json.md +++ b/_includes/api/en/4x/express.json.md @@ -40,12 +40,3 @@ The following table describes the properties of the optional `options` object. | `verify` | 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. | Function | `undefined` | - -#### Example: Different limits for specific routes - -```js -// Limit 5MB for /upload -app.use('/upload', express.json({ limit: '5mb' })) - -// Default limit for other routes -app.use(express.json()) \ No newline at end of file diff --git a/_includes/api/en/4x/express.static.md b/_includes/api/en/4x/express.static.md index b92c2d7b36..7db89cf051 100644 --- a/_includes/api/en/4x/express.static.md +++ b/_includes/api/en/4x/express.static.md @@ -93,3 +93,13 @@ var options = { app.use(express.static('public', options)) ``` +

Example of express.json

+ +Here is an example of using the `express.json` middleware function with a custom limit: + +```js +// Apply a 5MB limit for /upload requests +app.use('/upload', express.json({ limit: '5mb' })) + +// Use default limit for all other routes +app.use(express.json()) From 59d1bcfcc3413c4ca99b643fcacba0885ec523a3 Mon Sep 17 00:00:00 2001 From: kushal4345 <155578739+kushal4345@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:37:10 +0530 Subject: [PATCH 4/6] add express limit example in using middleware --- _includes/api/en/4x/express.static.md | 9 ---- zh-tw/guide/using-middleware.md | 64 +++++---------------------- 2 files changed, 12 insertions(+), 61 deletions(-) diff --git a/_includes/api/en/4x/express.static.md b/_includes/api/en/4x/express.static.md index 7db89cf051..4b532ca652 100644 --- a/_includes/api/en/4x/express.static.md +++ b/_includes/api/en/4x/express.static.md @@ -93,13 +93,4 @@ var options = { app.use(express.static('public', options)) ``` -

Example of express.json

-Here is an example of using the `express.json` middleware function with a custom limit: - -```js -// Apply a 5MB limit for /upload requests -app.use('/upload', express.json({ limit: '5mb' })) - -// Use default limit for all other routes -app.use(express.json()) diff --git a/zh-tw/guide/using-middleware.md b/zh-tw/guide/using-middleware.md index fe1770b024..fb4f5ec933 100644 --- a/zh-tw/guide/using-middleware.md +++ b/zh-tw/guide/using-middleware.md @@ -36,66 +36,26 @@ You can also load a series of middleware functions together, which creates a sub 使用 `app.use()` 和 `app.METHOD()` 函數,將應用程式層次的中介軟體連結至 [app object](/{{ page.lang }}/4x/api.html#app) 實例,其中 `METHOD` 是中介軟體函數要處理的 HTTP 要求方法(例如 GET、PUT 或 POST),並採小寫。 -This example shows a middleware function with no mount path. The function is executed every time the app receives a request. +

內建中介軟體

-```js -const express = require('express') -const app = express() - -app.use((req, res, next) => { - console.log('Time:', Date.now()) - next() -}) -``` - -This example shows a middleware function mounted on the `/user/:id` path. The function is executed for any type of -HTTP request on the `/user/:id` path. - -```js -app.use('/user/:id', (req, res, next) => { - console.log('Request Type:', req.method) - next() -}) -``` - -This example shows a route and its handler function (middleware system). The function handles GET requests to the `/user/:id` path. +Express has the following built-in middleware functions: -```js -app.get('/user/:id', (req, res, next) => { - res.send('USER') -}) -``` +- express.json() +- express.urlencoded() +- express.static() -Here is an example of loading a series of middleware functions at a mount point, with a mount path. -It illustrates a middleware sub-stack that prints request info for any type of HTTP request to the `/user/:id` path. +Here is an example of using the `express.json` middleware with a custom limit: ```js -app.use('/user/:id', (req, res, next) => { - console.log('Request URL:', req.originalUrl) - next() -}, (req, res, next) => { - console.log('Request Type:', req.method) - next() -}) -``` - -Route handlers enable you to define multiple routes for a path. The example below defines two routes for GET requests to the `/user/:id` path. The second route will not cause any problems, but it will never get called because the first route ends the request-response cycle. +const express = require('express') +const app = express() -本例顯示中介軟體子堆疊,它處理了指向 `/user/:id` 路徑的 GET 要求。 +// Apply a 5MB limit for /upload requests +app.use('/upload', express.json({ limit: '5mb' })) -```js -app.get('/user/:id', (req, res, next) => { - console.log('ID:', req.params.id) - next() -}, (req, res, next) => { - res.send('User Info') -}) +// Use default limit for all other routes +app.use(express.json()) -// handler for the /user/:id path, which prints the user ID -app.get('/user/:id', (req, res, next) => { - res.send(req.params.id) -}) -``` To skip the rest of the middleware functions from a router middleware stack, call `next('route')` to pass control to the next route. From ff39f9bae8e7f13ecace63cbc7c2192968a2f27f Mon Sep 17 00:00:00 2001 From: kushal4345 <155578739+kushal4345@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:48:57 +0530 Subject: [PATCH 5/6] fix the issue --- zh-tw/guide/using-middleware.md | 1 + 1 file changed, 1 insertion(+) diff --git a/zh-tw/guide/using-middleware.md b/zh-tw/guide/using-middleware.md index fb4f5ec933..2dc3e32d04 100644 --- a/zh-tw/guide/using-middleware.md +++ b/zh-tw/guide/using-middleware.md @@ -55,6 +55,7 @@ app.use('/upload', express.json({ limit: '5mb' })) // Use default limit for all other routes app.use(express.json()) +``` To skip the rest of the middleware functions from a router middleware stack, call `next('route')` to pass control to the next route. From e3cbb9e6f93ccfe558a9dac745f3954d7600dc94 Mon Sep 17 00:00:00 2001 From: kushal4345 <155578739+kushal4345@users.noreply.github.com> Date: Fri, 22 Aug 2025 08:48:51 +0530 Subject: [PATCH 6/6] fix issues --- en/guide/using-middleware.md | 14 +++++++++++++- zh-tw/guide/using-middleware.md | 22 ---------------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/en/guide/using-middleware.md b/en/guide/using-middleware.md index 0913c46b78..470d26f696 100644 --- a/en/guide/using-middleware.md +++ b/en/guide/using-middleware.md @@ -251,8 +251,20 @@ Express has the following built-in middleware functions: - [express.static](/en/4x/api.html#express.static) serves static assets such as HTML files, images, and so on. - [express.json](/en/4x/api.html#express.json) parses incoming requests with JSON payloads. **NOTE: Available with Express 4.16.0+** -- [express.urlencoded](/en/4x/api.html#express.urlencoded) parses incoming requests with URL-encoded payloads. **NOTE: Available with Express 4.16.0+** +- [express.urlencoded](/en/4x/api.html#express.urlencoded) parses incoming requests with URL-encoded payloads. **NOTE: Available with Express 4.16.0+** +Here is an example of using the `express.json` middleware with a custom limit: + +```js +const express = require('express') +const app = express() + +// Apply a 5MB limit for /upload requests +app.use('/upload', express.json({ limit: '5mb' })) + +// Use default limit for all other routes +app.use(express.json()) +```

Third-party middleware

Use third-party middleware to add functionality to Express apps. diff --git a/zh-tw/guide/using-middleware.md b/zh-tw/guide/using-middleware.md index 2dc3e32d04..8fc75db291 100644 --- a/zh-tw/guide/using-middleware.md +++ b/zh-tw/guide/using-middleware.md @@ -36,28 +36,6 @@ You can also load a series of middleware functions together, which creates a sub 使用 `app.use()` 和 `app.METHOD()` 函數,將應用程式層次的中介軟體連結至 [app object](/{{ page.lang }}/4x/api.html#app) 實例,其中 `METHOD` 是中介軟體函數要處理的 HTTP 要求方法(例如 GET、PUT 或 POST),並採小寫。 -

內建中介軟體

- -Express has the following built-in middleware functions: - -- express.json() -- express.urlencoded() -- express.static() - -Here is an example of using the `express.json` middleware with a custom limit: - -```js -const express = require('express') -const app = express() - -// Apply a 5MB limit for /upload requests -app.use('/upload', express.json({ limit: '5mb' })) - -// Use default limit for all other routes -app.use(express.json()) -``` - - To skip the rest of the middleware functions from a router middleware stack, call `next('route')` to pass control to the next route. {% capture next-function %}