hello i have noode js server project and i am trying to work with session using this tutorial https://scotch.io/tutorials/easy-node-authentication-setup-and-local and i get this error
TypeError: Cannot read property 'connect.sid' of undefined
at Layer.session [as handle] (C:\Users\Erez\node\test\node_modules\express-session\index.js:115:32)
at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17)
at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9
at Function.proto.process_params (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:251:12)
at Context.next (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:189:19)
at Context.actions.pass (C:\Users\Erez\node\test\node_modules\passport\lib\passport\context\http\actions.js:77:8)
at SessionStrategy.authenticate (C:\Users\Erez\node\test\node_modules\passport\lib\passport\strategies\session.js:67:10)
at attempt (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\authenticate.js:243:16)
at Passport.authenticate (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\authenticate.js:244:7)
at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17)
at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9
at Function.proto.process_params (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:251:12)
at next (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:189:19)
at Passport.initialize (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\initialize.js:69:5)
at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17)
at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9
here is my server.js
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var configDB = require('./config/database.js');
// configuration ===============================================================
mongoose.connect(configDB.url); // connect to our database
// required for passport
app.use(session({ secret: 'iloveschocolate'})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 3001;
// middleware to use for all requests
app.use(function(req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// do logging
next();
});
require('./Controllers/routes.js')(app, passport);
app.listen(port);
console.log('see whats happening on port ' + port);
i have tried many answers and nothing helped me i have try this Cannot read property 'connect.sid' of undefined at Layer.session - ExpressJS session and this Cannot read property 'connect.sid' of undefined at Layer.session - ExpressJS session and this nodejs express session error and also this document about connect - http://www.senchalabs.org/connect/session.html#session
when i remove this line app.use(session({ secret: 'iloveschocolate',})); // session secret
it works but it all the point to use this session? if i am right
i am using express 4 so i cant add express.cookie-parser thanks for the helpers
答案 0 :(得分:1)
也许是因为在初始化()
之前你还没有使用过cookiesparserapp.use(cookieParser());
答案 1 :(得分:0)
尝试在会话前放置cookieParser:
app.use(cookieParser());
app.use(session(
{
secret: 'appsecretkey',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
maxAge: new Date(Date.now() + 3600000)
}
}));