Skip to content

Commit 0082552

Browse files
krzysdzdougwilsondotnetCarpenterrhodgkinshematy61
authored
Update 5x API docs with missing 4x changes (#1886)
* Include 4.18 API doc updates in 5.x 5c98ee4 Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com> * Copy acceptsLanguages documentation improvements to 5.x #1402 Co-authored-by: Jon Ege Ronnenberg <jon.ronnenberg@gmail.com> * Add warning boxes to {app,res}.render 5e918ea Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com> * Copy warning around securing locals to 5.x fcaca7f Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com> * Copy res.cookie `partitioned` option docs #1456 Co-authored-by: Rich Hodgkins <rhodgkins@gmail.com> * Update req.body to point to built-in middleware a5ca5b0 Co-Authored-By: Douglas Wilson <doug@somethingdoug.com> * Copy setting multiple cookies example to 5.x #1063 Co-Authored-By: Mo <hematy61@gmail.com> --------- Co-authored-by: krzysdz <krzysdz@users.noreply.github.com> Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com> Co-authored-by: Jon Ege Ronnenberg <jon.ronnenberg@gmail.com> Co-authored-by: Rich Hodgkins <rhodgkins@gmail.com> Co-authored-by: Mo <hematy61@gmail.com>
1 parent 5c7a418 commit 0082552

File tree

8 files changed

+90
-22
lines changed

8 files changed

+90
-22
lines changed

_includes/api/en/5x/app-locals.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
The `app.locals` object has properties that are local variables within the application,
44
and will be available in templates rendered with [res.render](#res.render).
55

6+
<div class="doc-box doc-warn" markdown="1">
7+
The `locals` object is used by view engines to render a response. The object
8+
keys may be particularly sensitive and should not contain user-controlled
9+
input, as it may affect the operation of the view engine or provide a path to
10+
cross-site scripting. Consult the documentation for the used view engine for
11+
additional considerations.
12+
</div>
13+
614
```js
715
console.dir(app.locals.title)
816
// => 'My App'

_includes/api/en/5x/app-render.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ Think of `app.render()` as a utility function for generating rendered view strin
99
Internally `res.render()` uses `app.render()` to render views.
1010
</div>
1111

12+
<div class="doc-box doc-warn" markdown="1">
13+
The `view` argument performs file system operations like reading a file from
14+
disk and evaluating Node.js modules, and as so for security reasons should not
15+
contain input from the end-user.
16+
</div>
17+
18+
<div class="doc-box doc-warn" markdown="1">
19+
The `locals` object is used by view engines to render a response. The object
20+
keys may be particularly sensitive and should not contain user-controlled
21+
input, as it may affect the operation of the view engine or provide a path to
22+
cross-site scripting. Consult the documentation for the used view engine for
23+
additional considerations.
24+
</div>
25+
1226
<div class="doc-box doc-notice" markdown="1">
1327
The local variable `cache` is reserved for enabling view cache. Set it to `true`, if you want to
1428
cache view during development; view caching is enabled in production by default.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
<h3 id='req.acceptsLanguages'>req.acceptsLanguages(lang [, ...])</h3>
1+
<h3 id='req.acceptsLanguages'>req.acceptsLanguages([lang, ...])</h3>
22

33
Returns the first accepted language of the specified languages,
44
based on the request's `Accept-Language` HTTP header field.
55
If none of the specified languages is accepted, returns `false`.
66

7+
If no `lang` argument is given, then `req.acceptsLanguages()`
8+
returns all languages from the HTTP `Accept-Language` header
9+
as an `Array`.
10+
711
For more information, or if you have issues or concerns, see [accepts](https://github.com/expressjs/accepts).
12+
13+
Express (5.x) source: [request.js line 172](https://github.com/expressjs/express/blob/v5.1.0/lib/request.js#L172)
14+
15+
Accepts (2.0) source: [index.js line 195](https://github.com/jshttp/accepts/blob/2.0.0/index.js#L195)

_includes/api/en/5x/req-body.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Contains key-value pairs of data submitted in the request body.
44
By default, it is `undefined`, and is populated when you use body-parsing middleware such
5-
as [body-parser](https://www.npmjs.org/package/body-parser) and [multer](https://www.npmjs.org/package/multer).
5+
as [`express.json()`](#express.json) or [`express.urlencoded()`](#express.urlencoded).
66

77
<div class="doc-box doc-warn" markdown="1">
88
As `req.body`'s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, `req.body.foo.toString()` may fail in multiple ways, for example `foo` may not be there or may not be a string, and `toString` may not be a function and instead a string or other user-input.
@@ -11,15 +11,14 @@ As `req.body`'s shape is based on user-controlled input, all properties and valu
1111
The following example shows how to use body-parsing middleware to populate `req.body`.
1212

1313
```js
14-
const app = require('express')()
15-
const bodyParser = require('body-parser')
16-
const multer = require('multer') // v1.0.5
17-
const upload = multer() // for parsing multipart/form-data
14+
const express = require('express')
1815

19-
app.use(bodyParser.json()) // for parsing application/json
20-
app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
16+
const app = express()
2117

22-
app.post('/profile', upload.array(), (req, res, next) => {
18+
app.use(express.json()) // for parsing application/json
19+
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
20+
21+
app.post('/profile', (req, res, next) => {
2322
console.log(req.body)
2423
res.json(req.body)
2524
})

_includes/api/en/5x/res-cookie.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ Sets cookie `name` to `value`. The `value` parameter may be a string or object
44

55
The `options` parameter is an object that can have the following properties.
66

7-
| Property | Type | Description |
8-
|-------------|-------------------------------------------------------------------------|
9-
| `domain` | String | Domain name for the cookie. Defaults to the domain name of the app.
10-
| `encode` | Function | A synchronous function used for cookie value encoding. Defaults to `encodeURIComponent`.
11-
| `expires` | Date | Expiry date of the cookie in GMT. If not specified or set to 0, creates a session cookie.
12-
| `httpOnly` | Boolean | Flags the cookie to be accessible only by the web server.
13-
| `maxAge` | Number | Convenient option for setting the expiry time relative to the current time in milliseconds.
14-
| `path` | String | Path for the cookie. Defaults to "/".
15-
| `secure` | Boolean | Marks the cookie to be used with HTTPS only.
16-
| `signed` | Boolean | Indicates if the cookie should be signed.
17-
| `sameSite` | Boolean or String | Value of the "SameSite" **Set-Cookie** attribute. More information at [https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1](https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1).
7+
| Property | Type | Description |
8+
|---------------|-------------------------------------------------------------------------|
9+
| `domain` | String | Domain name for the cookie. Defaults to the domain name of the app.
10+
| `encode` | Function | A synchronous function used for cookie value encoding. Defaults to `encodeURIComponent`.
11+
| `expires` | Date | Expiry date of the cookie in GMT. If not specified or set to 0, creates a session cookie.
12+
| `httpOnly` | Boolean | Flags the cookie to be accessible only by the web server.
13+
| `maxAge` | Number | Convenient option for setting the expiry time relative to the current time in milliseconds.
14+
| `path` | String | Path for the cookie. Defaults to "/".
15+
| `partitioned` | Boolean | Indicates that the cookie should be stored using partitioned storage. See [Cookies Having Independent Partitioned State (CHIPS)](https://developer.mozilla.org/en-US/docs/Web/Privacy/Partitioned_cookies) for more details.
16+
| `priority` | String | Value of the "Priority" **Set-Cookie** attribute.
17+
| `secure` | Boolean | Marks the cookie to be used with HTTPS only.
18+
| `signed` | Boolean | Indicates if the cookie should be signed.
19+
| `sameSite` | Boolean or String | Value of the "SameSite" **Set-Cookie** attribute. More information at [https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1](https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1).
1820

1921
<div class="doc-box doc-notice" markdown="1">
2022
All `res.cookie()` does is set the HTTP `Set-Cookie` header with the options provided.
@@ -28,6 +30,18 @@ res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: tru
2830
res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true })
2931
```
3032

33+
You can set multiple cookies in a single response by calling `res.cookie` multiple times, for example:
34+
35+
```js
36+
res
37+
.status(201)
38+
.cookie('access_token', `Bearer ${token}`, {
39+
expires: new Date(Date.now() + 8 * 3600000) // cookie will be removed after 8 hours
40+
})
41+
.cookie('test', 'test')
42+
.redirect(301, '/admin')
43+
```
44+
3145
The `encode` option allows you to choose the function used for cookie value encoding.
3246
Does not support asynchronous functions.
3347

_includes/api/en/5x/res-download.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ The optional `options` argument is supported by Express v4.16.0 onwards.
66

77
Transfers the file at `path` as an "attachment". Typically, browsers will prompt the user for download.
88
By default, the `Content-Disposition` header "filename=" parameter is derived from the `path` argument, but can be overridden with the `filename` parameter.
9-
If `path` is relative, then it will be based on the current working directory of the process.
9+
If `path` is relative, then it will be based on the current working directory of the process or
10+
the `root` option, if provided.
11+
12+
<div class="doc-box doc-warn" markdown="1">
13+
This API provides access to data on the running file system. Ensure that either (a) the way in
14+
which the `path` argument was constructed is secure if it contains user input or (b) set the `root`
15+
option to the absolute path of a directory to contain access within.
16+
17+
When the `root` option is provided, Express will validate that the relative path provided as
18+
`path` will resolve within the given `root` option.
19+
</div>
1020

1121
The following table provides details on the `options` parameter.
1222

@@ -19,6 +29,7 @@ The optional `options` argument is supported by Express v4.16.0 onwards.
1929
| Property | Description | Default | Availability |
2030
|-----------------|-------------------------------------------------|-------------|--------------|
2131
| `maxAge` | Sets the max-age property of the `Cache-Control` header in milliseconds or a string in [ms format](https://www.npmjs.org/package/ms)| 0 | 4.16+ |
32+
| `root` | Root directory for relative filenames.| | 4.18+ |
2233
| `lastModified` | Sets the `Last-Modified` header to the last modified date of the file on the OS. Set `false` to disable it.| Enabled | 4.16+ |
2334
| `headers` | Object containing HTTP headers to serve with the file. The header `Content-Disposition` will be overridden by the `filename` argument.| | 4.16+ |
2435
| `dotfiles` | Option for serving dotfiles. Possible values are "allow", "deny", "ignore".| "ignore" | 4.16+ |

_includes/api/en/5x/res-locals.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Use this property to set variables accessible in templates rendered with [res.re
44
The variables set on `res.locals` are available within a single request-response cycle, and will not
55
be shared between requests.
66

7+
<div class="doc-box doc-warn" markdown="1">
8+
The `locals` object is used by view engines to render a response. The object
9+
keys may be particularly sensitive and should not contain user-controlled
10+
input, as it may affect the operation of the view engine or provide a path to
11+
cross-site scripting. Consult the documentation for the used view engine for
12+
additional considerations.
13+
</div>
14+
715
In order to keep local variables for use in template rendering between requests, use
816
[app.locals](#app.locals) instead.
917

_includes/api/en/5x/res-render.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ The `view` argument is a string that is the file path of the view file to render
1010

1111
For more information, see [Using template engines with Express](/{{page.lang}}/guide/using-template-engines.html).
1212

13-
{% include admonitions/note.html content="The `view` argument performs file system operations like reading a file from disk and evaluating Node.js modules, and as so for security reasons should not contain input from the end-user." %}
13+
{% include admonitions/warning.html content="The `view` argument performs file system operations like reading a file from disk and evaluating Node.js modules, and as so for security reasons should not contain input from the end-user." %}
14+
15+
{% include admonitions/warning.html content="The `locals` object is used by view engines to render a response. The object
16+
keys may be particularly sensitive and should not contain user-controlled
17+
input, as it may affect the operation of the view engine or provide a path to
18+
cross-site scripting. Consult the documentation for the used view engine for
19+
additional considerations." %}
1420

1521
{% include admonitions/caution.html content="The local variable `cache` enables view caching. Set it to `true`,
1622
to cache the view during development; view caching is enabled in production by default." %}

0 commit comments

Comments
 (0)