Parse 解析Cookie
header and populate req.cookies
with an object keyed by the cookie names. Cookie
头并用Cookie名称键控的对象填充req.cookies
。Optionally 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
,以便其他中间件可以使用它。
$ npm install cookie-parser
var cookieParser = require('cookie-parser')
Create a new cookie parser middleware function using the given 使用给定的secret
and options
.secret
和options
创建一个新的cookie解析器中间件函数。
secret
options
cookie.parse
as the second option. s
的对象。decode
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.signedCookies
。A signed cookie is a cookie that has a value prefixed with 签名cookie是具有前缀为s:
. s:
的值的cookie。Signed cookies that fail signature validation will have the value 签名验证失败的签名Cookie的值将为false
instead of the tampered value.false
,而不是篡改的值。
In addition, this module supports special "JSON cookies". 此外,该模块还支持特殊的“JSON cookies”。These are cookie where the value is prefixed with 这些是cookie,其中值的前缀为j:
. 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.如果解析失败,将保留原始值。
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值,否则返回传递的值。
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.这将返回传入的相同对象。
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取消签名。
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取消签名。
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"