I am getting the following errors on my website when I load it on an online server. I using express 3.x, the first url don´t has probems, but i do login the error is
TypeError: Cannot set property 'user' of undefined at
My code is:
server.engine('html', swig.renderFile);
server.set('view engine', 'html');
server.set('views', './app/views');
server.configure(function() {
server.use(express.logger());
server.use(express.cookieParser());
server.use(express.bodyParser());
server.use(express.session({
secret: "lolcatz",
store: new RedisStore({})
}));
});
server.get('/', function (req, res){
res.render('home');
});
server.get('/app', function (req, res) {
res.render('app', {
user: req.session.user
});
});
server.post('/log-in', function (req, res) {
req.session.user = req.body.username;
res.redirect('/app');
});
server.listen(3000);
Please state software version your issue is occurring.sometimes version is the matter and I guess this time as well.
first of all, the error is happening all because of req.session.user = req.body.username;
which means you do not have properly set
server.use(express.session({
secret: "lolcatz",
store: new RedisStore({})
}));
Though above code should work.So I assume you are using express 4.0 and keep using server.configure function. Since app.configure in express version 3.x was removed in express version 4.0,The code above would not work in express 4.0
so simply remove the function and make it something like...
server.use(express.logger());
server.use(express.cookieParser());
server.use(express.bodyParser());
server.use(express.session({
secret: "lolcatz",
store: new RedisStore({})
}));
server.get('/', function (req, res){
res.render('home');