-
Notifications
You must be signed in to change notification settings - Fork 1.9k
docs(express.json): clarify usage and add route-specific limit example #2037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
b6c6192
0e581e1
7940a18
59d1bcf
ff39f9b
e3cbb9e
3424f61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,3 +93,4 @@ var options = { | |
|
||
app.use(express.static('public', options)) | ||
``` | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -252,7 +252,20 @@ Express has the following built-in middleware functions: | |||
- [express.static](/en/5x/api.html#express.static) serves static assets such as HTML files, images, and so on. | ||||
- [express.json](/en/5x/api.html#express.json) parses incoming requests with JSON payloads. **NOTE: Available with Express 4.16.0+** | ||||
- [express.urlencoded](/en/5x/api.html#express.urlencoded) parses incoming requests with URL-encoded payloads. **NOTE: Available with Express 4.16.0+** | ||||
gh-pages | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
cleanup |
||||
|
||||
Here is an example of using the `express.json` middleware with a custom limit: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Should we need separate pages for explaining how to use built in middleware options? |
||||
|
||||
```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()) | ||||
``` | ||||
<h2 id='middleware.third-party'>Third-party middleware</h2> | ||||
|
||||
Use third-party middleware to add functionality to Express apps. | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this file neither, it’s in this one. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,67 +36,6 @@ You can also load a series of middleware functions together, which creates a sub | |
|
||
Bind application-level middleware to an instance of the [app object](/{{ page.lang }}/5x/api.html#app) by using the `app.use()` and `app.METHOD()` functions, where `METHOD` is the HTTP method of the request that the middleware function handles (such as GET, PUT, or POST) in lowercase. | ||
|
||
This example shows a middleware function with no mount path. The function is executed every time the app receives a request. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are not required. Please undo it. |
||
|
||
```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. | ||
|
||
```js | ||
app.get('/user/:id', (req, res, next) => { | ||
res.send('USER') | ||
}) | ||
``` | ||
|
||
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. | ||
|
||
```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. | ||
|
||
本例顯示中介軟體子堆疊,它處理了指向 `/user/:id` 路徑的 GET 要求。 | ||
|
||
```js | ||
app.get('/user/:id', (req, res, next) => { | ||
console.log('ID:', req.params.id) | ||
next() | ||
}, (req, res, next) => { | ||
res.send('User Info') | ||
}) | ||
|
||
// 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. | ||
|
||
{% capture next-function %} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can see, you made the change in the wrong file, judging by the commit message.