无法在 Express Route 中间件中设置会话属性
我目前正在使用 NodeJS 和 Express 开发 API。
var express = require('express');
var app = express.createServer();
app.configure(function() {
app.use(express.cookieParser());
app.use(express.session({ secret:"aaa"}));
});
function tokenLogin (req, res) {
req.sesssion.uid = 1;
}
app.get('/api/news/:inbox', tokenLogin, function(req, res) {
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
当我调用路由 /api/news/1
时,控制台显示 TypeError:
<代码>
TypeError: Cannot set property 'uid' of undefined
at tokenLogin (/Volumes/Dev/a/test.js:13:19)
at callbacks (/Volumes/Dev/a/node_modules/express/lib/router/index.js:272:11)
at param (/Volumes/Dev/a/node_modules/express/lib/router/index.js:246:11)
at param (/Volumes/Dev/a/node_modules/express/lib/router/index.js:243:11)
at pass (/Volumes/Dev/a/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/Volumes/Dev/a/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/Volumes/Dev/a/node_modules/express/lib/router/index.js:45:10)
at next (/Volumes/Dev/a/node_modules/express/node_modules/connect/lib/http.js:203:15)
at /Volumes/Dev/a/node_modules/express/node_modules/connect/lib/middleware/session.js:323:9
at /Volumes/Dev/a/node_modules/express/node_modules/connect/lib/middleware/session.js:338:9
这是我的代码中的错误还是 ExpressJS 的错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
req.sesssion
是一个拼写错误。您需要req.session
。req.sesssion
is a typo. You wantreq.session
.