无法读取属性' connect.sid'在Layer.session中未定义 - ExpressJS会话

时间:2014-09-29 19:30:06

标签: node.js session express passport.js

我在ExpressJS 4中使用会话时遇到问题。

我尝试在server.js(我设置应用程序的配置)和routes.js(我定义路由)中添加会话。这就是我不断得到的:

TypeError: Cannot read property 'connect.sid' of undefined at Layer.session [as handle]
(/Users/larissaleite/Documents/Routing/node_modules/express-session/index.js:115:32) at trim_prefix 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:226:17) at c 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:198:9) at 
Function.proto.process_params 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:251:12) at next 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:189:19) at 
Layer.expressInit [as handle] 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/middleware/init.js:23:5) at 
trim_prefix (/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:226:17) 
at c (/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:198:9) at 
Function.proto.process_params 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:251:12) at next 
(/Users/larissaleite/Documents/Routing/node_modules/express/lib/router/index.js:189:19)

我定义了这样的会话(server.js):

var express  = require('express');
var app      = express();                                   // create app with express
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');

var passport = require('passport');

var session = require('express-session');

connect()
  .use(cookieParser('appsecret'))
  .use(function(req, res, next){
   res.end(JSON.stringify(req.cookies));
});

app.use(session({ secret: 'appsecret', saveUninitialized: true, cookie: { secure: true, maxAge: new Date(Date.now() + 3600000) }, key:'connect.sid' }));

我也尝试删除密钥。

当尝试在routes.js中定义会话时,它是这样的:

app.use(session({ secret: 'appsecret', saveUninitialized: true, cookie: { secure: true, maxAge: new Date(Date.now() + 3600000) }, key:'connect.sid' }));

app.use(passport.initialize());
app.use(passport.session());

2 个答案:

答案 0 :(得分:3)

全部删除连接。 Express 4是一项重大改革,将连接作为一种依赖关系。删除所有需要/呼叫连接的部分。还要更新所有依赖项。

根据快递会议中间件模块的文档: name - cookie name (formerly known as key). (default: 'connect.sid')

不要传递任何东西,它将使用快递sid。我已复制您的代码并在没有连接的情况下对其进行了测试,但我没有收到这些错误。另外,我已将resave密钥添加到session配置对象,以便将已弃用的警告静音(如果您正在运行最新的快递4版本)。

var express = require('express');
var app = express(); // create app with express
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');

var passport = require('passport');

var session = require('express-session');

app.use(session({
  secret: 'appsecret',
  resave: false,
  saveUninitialized: true,
  cookie: {
    secure: true,
    maxAge: new Date(Date.now() + 3600000)
  }
}));

app.listen(1234);

答案 1 :(得分:0)

  

首先将您的节点版本更新为最新版本:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable
  

更新所有依赖包:

npm i -g npm-check-updates
ncu -u
npm install
  

如果需要

npm audit fix