Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _includes/api/en/4x/express.json.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ 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` |

</div>
</div>
1 change: 1 addition & 0 deletions _includes/api/en/4x/express.static.md
Copy link
Member

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.

Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ var options = {

app.use(express.static('public', options))
```

63 changes: 12 additions & 51 deletions zh-tw/guide/using-middleware.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
Expand Up @@ -36,66 +36,27 @@ 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.
<h2 id='middleware.built-in'>內建中介軟體</h2>

```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.
Express has the following built-in middleware functions:

```js
app.use('/user/:id', (req, res, next) => {
console.log('Request Type:', req.method)
next()
})
```
- express.json()
- express.urlencoded()
- express.static()

This example shows a route and its handler function (middleware system). The function handles GET requests to the `/user/:id` path.
Here is an example of using the `express.json` middleware with a custom limit:

```js
app.get('/user/:id', (req, res, next) => {
res.send('USER')
})
```
const express = require('express')
const app = express()

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.
// Apply a 5MB limit for /upload requests
app.use('/upload', express.json({ limit: '5mb' }))

```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()
})
// Use default limit for all other routes
app.use(express.json())
```

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.

Expand Down