无法在 Express Route 中间件中设置会话属性

发布于 2024-12-17 04:12:15 字数 1511 浏览 0 评论 0 原文

我目前正在使用 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 的错误?

I'm currently developing an API using NodeJS and Express.



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

When I call the route /api/news/1, the console shows a 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

Is this a bug on my code or is an ExpressJS' bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-12-24 04:12:15

req.sesssion 是一个拼写错误。您需要req.session

req.sesssion is a typo. You want req.session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文