cookie-parser

NPM Version NPM Downloads Build Status Test Coverage

Parse Cookie header and populate req.cookies with an object keyed by the cookie names. 解析Cookie头并用Cookie名称键控的对象填充req.cookiesOptionally you may enable signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware.或者,您可以通过传递一个secret字符串来启用签名cookie支持,该字符串分配req.secret,以便其他中间件可以使用它。

Installation安装

$ npm install cookie-parser

API

var cookieParser = require('cookie-parser')

cookieParser(secret, options)

Create a new cookie parser middleware function using the given secret and options.使用给定的secretoptions创建一个新的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. 中间件将解析请求上的Cookie头,并将Cookie数据作为属性req.cookies公开,如果提供了secret,则作为属性req.signedCookies公开。These properties are name value pairs of the cookie name to cookie value.这些属性是cookie名称和cookie值之间的名值对。

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. 当提供secret时,此模块将取消签名并验证任何已签名cookie值,并将这些名值对从req.cookies移动到req.signedCookiesA signed cookie is a cookie that has a value prefixed with s:. 签名cookie是具有前缀为s:的值的cookie。Signed cookies that fail signature validation will have the value false instead of the tampered value.签名验证失败的签名Cookie的值将为false,而不是篡改的值。

In addition, this module supports special "JSON cookies". 此外,该模块还支持特殊的“JSON cookies”。These are cookie where the value is prefixed with j:. 这些是cookie,其中值的前缀为j:When these values are encountered, the value will be exposed as the result of JSON.parse. 遇到这些值时,该值将作为JSON.parse的结果公开。If parsing fails, the original value will remain.如果解析失败,将保留原始值。

cookieParser.JSONCookie(str)

Parse a cookie value as a JSON cookie. 将cookie值解析为JSON cookie。This will return the parsed JSON value if it was a JSON cookie, otherwise, it will return the passed value.如果是JSON cookie,则返回解析后的JSON值,否则返回传递的值。

cookieParser.JSONCookies(cookies)

Given an object, this will iterate over the keys and call JSONCookie on each value, replacing the original value with the parsed value. 给定一个对象,它将迭代键并对每个值调用JSONCOKIE,用解析的值替换原始值。This returns the same object that was passed in.这将返回传入的相同对象。

cookieParser.signedCookie(str, secret)

Parse a cookie value as a signed cookie. 将cookie值解析为签名cookie。This will return the parsed unsigned value if it was a signed cookie and the signature was valid. 如果解析后的无符号值是有符号cookie且签名有效,则返回该值。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.如果值已签名但签名无法验证,则返回false

The secret argument can be an array or string. secret参数可以是数组或字符串。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.如果提供了数组,则将尝试按顺序对cookie取消签名。

cookieParser.signedCookies(cookies, secret)

Given an object, this will iterate over the keys and check if any value is a signed cookie. 给定一个对象,这将迭代键并检查是否有任何值是签名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.如果是已签名的cookie且签名有效,则将从对象中删除密钥并将其添加到返回的新对象中。

The secret argument can be an array or string. secret参数可以是数组或字符串。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.如果提供了数组,则将尝试按顺序对cookie取消签名。

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