20

我使用以下内容使用 express 设置 cookie。

res.cookie('test', 'yes', { 
    expires: new Date(Date.now() + 365*2*24*60*60*1000), 
    httpOnly: true 
});

我正在使用中间件来尝试读取 cookie:

app.use(express.cookieParser());

var cookie_id = req.cookies.test;

问题是 cookie_id 是未定义的,它会是“是”

Express server listening on port 8060 in development mode
TypeError: Cannot read property 'test' of undefined
    at /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/app.js:48:29
    at callbacks (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:272:11)
    at param (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:246:11)
    at pass (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:280:4)
    at Object.handle (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:45:10)
    at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Object.methodOverride [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
    at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Object.bodyParser [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:88:61)

我做错了什么?

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  test=yes
Host    127.0.0.1:8060
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0
// Configuration
app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
  app.use(express.cookieParser());
});
4

3 回答 3

35

express.cookieParser() 之前 需要使用app.router;中间件按顺序运行,这意味着它甚至cookieParser()在您的路线执行之前永远不会到达。

尝试这个:

  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
于 2012-05-18T08:35:15.563 回答
13

大多数中间件(如 cookieParser)不再与 Express 捆绑,必须单独安装。

安装 cookie 解析器

npm install cookie-parser

用法

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

var app = express()
app.use(cookieParser())

https://github.com/expressjs/cookie-parser

于 2016-01-02T12:58:39.057 回答
1

我通过app.use(express.cookieParser());app.router.

于 2013-12-18T20:47:10.117 回答