res.cookie(name, value [, options])

将 cookie name 设置为 valuevalue 参数可以是字符串或转换为 JSON 的对象。

Sets cookie name to value. The value parameter may be a string or object converted to JSON.

options 参数是一个可以具有以下属性的对象。

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

属性

Property

类型

Type

描述

Description

domain字符串

String

cookie 的域名。默认为应用的域名。

Domain name for the cookie. Defaults to the domain name of the app.

encode函数

Function

用于 cookie 值编码的同步函数。默认为 encodeURIComponent

A synchronous function used for cookie value encoding. Defaults to encodeURIComponent.

expires日期

Date

格林威治标准时间 cookie 的到期日期。如果未指定或设置为 0,则创建会话 cookie。

Expiry date of the cookie in GMT. If not specified or set to 0, creates a session cookie.

httpOnly布尔值

Boolean

将 cookie 标记为只能由 Web 服务器访问。

Flags the cookie to be accessible only by the web server.

maxAge数字

Number

方便的选项,用于设置相对于当前时间的到期时间(以毫秒为单位)。

Convenient option for setting the expiry time relative to the current time in milliseconds.

path字符串

String

cookie 的路径。默认为 "/"。

Path for the cookie. Defaults to "/".

secure布尔值

Boolean

将 cookie 标记为仅与 HTTPS 一起使用。

Marks the cookie to be used with HTTPS only.

signed布尔值

Boolean

指示是否应该对 cookie 进行签名。

Indicates if the cookie should be signed.

sameSite布尔值或字符串

Boolean or String

"SameSite" Set-Cookie 属性的值。更多信息请参见 https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1

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.

res.cookie() 所做的只是使用提供的选项设置 HTTP Set-Cookie 标头。任何未指定的选项默认为 RFC 6265 中规定的值。

All res.cookie() does is set the HTTP Set-Cookie header with the options provided. Any option not specified defaults to the value stated in RFC 6265.

例如:

For example:

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true })
res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true })

encode 选项允许您选择用于 cookie 值编码的函数。不支持异步函数。

The encode option allows you to choose the function used for cookie value encoding. Does not support asynchronous functions.

示例用例:您需要为组织中的另一个站点设置域范围的 cookie。此其他站点(不受您的管理控制)不使用 URI 编码的 cookie 值。

Example use case: You need to set a domain-wide cookie for another site in your organization. This other site (not under your administrative control) does not use URI-encoded cookie values.

// Default encoding
res.cookie('some_cross_domain_cookie', 'http://mysubdomain.example.com', { domain: 'example.com' })
// Result: 'some_cross_domain_cookie=http%3A%2F%2Fmysubdomain.example.com; Domain=example.com; Path=/'

// Custom encoding
res.cookie('some_cross_domain_cookie', 'http://mysubdomain.example.com', { domain: 'example.com', encode: String })
// Result: 'some_cross_domain_cookie=http://mysubdomain.example.com; Domain=example.com; Path=/;'

maxAge 选项是一个方便的选项,用于设置 "expires" 相对于当前时间(以毫秒为单位)。以下等效于上面的第二个示例。

The maxAge option is a convenience option for setting "expires" relative to the current time in milliseconds. The following is equivalent to the second example above.

res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })

您可以将对象作为 value 参数传递;然后将其序列化为 JSON 并由 bodyParser() 中间件解析。

You can pass an object as the value parameter; it is then serialized as JSON and parsed by bodyParser() middleware.

res.cookie('cart', { items: [1, 2, 3] })
res.cookie('cart', { items: [1, 2, 3] }, { maxAge: 900000 })

使用 cookie-parser 中间件时,此方法还支持签名 cookie。只需将 signed 选项设置为 true。然后 res.cookie() 将使用传递给 cookieParser(secret) 的秘密对值进行签名。

When using cookie-parser middleware, this method also supports signed cookies. Simply include the signed option set to true. Then res.cookie() will use the secret passed to cookieParser(secret) to sign the value.

res.cookie('name', 'tobi', { signed: true })

稍后您可以通过 req.signedCookies 对象访问此值。

Later you may access this value through the req.signedCookies object.