本文介绍了Node.js和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服务器在开发模式下侦听端口8060 
TypeError:无法读取未定义的属性test
在/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/app.js:48:29
在回调(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / nodeServer / node_modules / express / lib /router/index.js:272:11)
在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)
在Router._dispatch(/ home / ubuntu /workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/lib/router/index.js:280:4)
在Object.handle(/ home / ubun tu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / nodeServer / node_modules / express / lib / router / index.js:45:10)
在下一个(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / nodeServer / (/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / nodeServer / node_modules / express / ...) node_modules / connect / lib / middleware / methodOverride.js:35:5)
在下一个(/ home / ubuntu / workspace / rtbopsConfig / rtbServers / rtbNodejsServer / nodeServer / node_modules / express / node_modules / connect / lib / http。 js:204:15)
在Object.bodyParser [as handle](/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/nodeServer/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js: 88:61)

我做错了什么?

 接受文本/ html,application / xhtml + xml,application / xml; q = 0.9,* /*;q=0.8 
接受编码gzip,deflate
接受语言en-us,en; q = 0.5
连接保持活动
Cookie测试=是
主机127.0.0.1:8060
用户代理Mozilla / 5.0(X11; Ubuntu的; Linux x86_64; rv:12.0)Gecko / 20100101 Firefox / 12.0





  //配置
app.configure(function(){
app.set('views',__dirname +'/ views');
app.set('查看引擎','jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router );
app.use(express.static(__ dirname +'/ public'));
app.use(express.cookieParser());
});


解决方案

您需要使用 express .cookieParser() before 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'));


I use the below to set a cookie using express.

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

I am using the middleware to try and read the cookie:

app.use(express.cookieParser());

var cookie_id = req.cookies.test;

Problem is that cookie_id is undefined whne it would be 'yes'

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)

What did I do wrong?

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());
});
解决方案

You need to use express.cookieParser() before app.router; middleware is run in order, meaning it's never even reaching cookieParser() before your route is executed.

Try this:

  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'));

这篇关于Node.js和express - 如何读取cookie但未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:39