无法在Layer.session中读取未定义的属性'connect.sid'-ExpressJS会话 [英] Cannot read property 'connect.sid' of undefined at Layer.session - ExpressJS session

查看:120
本文介绍了无法在Layer.session中读取未定义的属性'connect.sid'-ExpressJS会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having problems using sessions in ExpressJS 4.

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

I have tried adding session both in server.js (where I set my app's configurations) and in routes.js (where I define my routes). This is what I keep getting:

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

I defined the session like this (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中定义会话时,就像这样:

When trying to define the session in routes.js, it was like this:

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

推荐答案

删除所有连接在一起的内容. Express 4进行了大修,取消了connect的依赖关系.删除所有需要/调用连接的部件.同时更新所有依赖项.

Remove connect all together. Express 4 was a big overhaul that removed connect as a dependency. remove all parts that require / call connect. Also update all your dependencies.

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

Per the express-session middleware module's docs: name - cookie name (formerly known as key). (default: 'connect.sid')

请勿传递任何内容,它将使用快速sid.我已经复制了您的代码并在没有连接的情况下对其进行了测试,并且没有得到这些错误.另外,我已经在session配置对象中添加了resave键,以使已弃用的警告静音(如果您正在运行最新的express 4版本).

Don't pass it anything and it will use the express sid. I've copied your code and tested it without connect and I'm not getting those errors. Also, I've added the resave key to the session config object to mute the deprecated warnings (if you're running the latest express 4 version).

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

这篇关于无法在Layer.session中读取未定义的属性'connect.sid'-ExpressJS会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆