🌐 Nodejs.cn

cookie-parser 中间件

解析 Cookie 头并用一个以 cookie 名称为键的对象填充 req.cookies。可选地,你可以通过传递一个 secret 字符串来启用签名 cookie 支持,这会将 req.secret 分配,以便其他中间件可以使用它。

🌐 Parse Cookie header and populate req.cookies with an object keyed by the cookie names. Optionally you may enable signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware.

安装

🌐 Installation

Terminal window
npm install cookie-parser

Note

cookie-parser 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/cookie-parser

API

var cookieParser = require('cookie-parser');

cookieParser(secret, options)

使用给定的 secretoptions 创建一个新的 cookie 解析中间件函数。

🌐 Create a new cookie parser middleware function using the given secret and options.

  • secret 用于签名 cookie 的字符串或数组。这是可选的,如果未指定,将不会解析签名的 cookie。如果提供的是字符串,则将其用作密钥。如果提供的是数组,则会尝试按顺序使用数组中的每个密钥对 cookie 进行解签名。
  • options 是作为第二个选项传递给 cookie.parse 的对象。更多信息请参见 cookie
    • decode 是一个用来解码 cookie 值的函数

中间件将解析请求中的 Cookie 头,并将 cookie 数据作为属性 req.cookies 暴露,如果提供了 secret,也会作为属性 req.signedCookies 暴露。这些属性是 cookie 名称到 cookie 值的名称值对。

🌐 The middleware will parse the Cookie header on the request and expose the cookie data as the property req.cookies and, if a secret was provided, as the property req.signedCookies. These properties are name value pairs of the cookie name to cookie value.

当提供 secret 时,此模块将对任何已签名的 cookie 值进行去签名和验证,并将这些名称值对从 req.cookies 移动到 req.signedCookies。 已签名的 cookie 是其值以 s: 为前缀的 cookie。签名验证失败的已签名 cookie 将用 false 作为值,而不是篡改后的值。

🌐 When secret is provided, this module will unsign and validate any signed cookie values and move those name value pairs from req.cookies into req.signedCookies. A signed cookie is a cookie that has a value prefixed with s:. Signed cookies that fail signature validation will have the value false instead of the tampered value.

此外,该模块支持特殊的“JSON cookies”。这些 cookies 的值以 j: 为前缀。当遇到这些值时,该值将作为 JSON.parse 的结果公开。如果解析失败,原始值将保持不变。

🌐 In addition, this module supports special “JSON cookies”. These are cookie where the value is prefixed with j:. When these values are encountered, the value will be exposed as the result of JSON.parse. If parsing fails, the original value will remain.

cookieParser.JSONCookie(str)

将 cookie 值解析为 JSON cookie。如果它是 JSON cookie,这将返回解析后的 JSON 值,否则将返回传入的值。

🌐 Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise, it will return the passed value.

cookieParser.JSONCookies(cookies)

给定一个对象,这将遍历其键并对每个值调用 JSONCookie,将原始值替换为解析后的值。它返回传入的同一个对象。

🌐 Given an object, this will iterate over the keys and call JSONCookie on each value, replacing the original value with the parsed value. This returns the same object that was passed in.

cookieParser.signedCookie(str, secret)

将 cookie 值解析为签名 cookie。如果它是签名 cookie 并且签名有效,将返回解析后的未签名值。如果该值未签名,则返回原始值。如果该值已签名但无法验证签名,则返回 false

🌐 Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature was valid. If the value was not signed, the original value is returned. If the value was signed but the signature could not be validated, false is returned.

secret 参数可以是数组或字符串。如果提供的是字符串,则将其用作密钥。如果提供的是数组,将尝试按顺序使用数组中的每个密钥对 cookie 进行解签名。

🌐 The secret argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each secret in order.

cookieParser.signedCookies(cookies, secret)

给定一个对象,这将遍历其键并检查是否有任何值是签名的 cookie。如果是签名的 cookie 并且签名有效,该键将从对象中删除并添加到返回的新对象中。

🌐 Given an object, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature is valid, the key will be deleted from the object and added to the new object that is returned.

secret 参数可以是数组或字符串。如果提供的是字符串,则将其用作密钥。如果提供的是数组,将尝试按顺序使用数组中的每个密钥对 cookie 进行解签名。

🌐 The secret argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each secret in order.

示例

🌐 Example

var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.get('/', function (req, res) {
// Cookies that have not been signed
console.log('Cookies: ', req.cookies);
// Cookies that have been signed
console.log('Signed Cookies: ', req.signedCookies);
});
app.listen(8080);
// curl command that sends an HTTP request with two cookies
// curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello"

许可证

🌐 License

MIT