cookie-session 无法设置会话
cookie-session cannot set session
我的问题是关于 cookie 会话的。所以我在尝试处理 cookie-session 后遇到了这个问题。我无法设置任何会话。问题是当我试图设置它时 req.session.login
而这个 login
是个问题但我不知道为什么
process.nextTick(function() { throw err; });
^
TypeError: Cannot set property 'login' of undefined
at C:\Users\syriu\WebstormProjects\pubg\routes\login.js:30:35
这是我的app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cookieSession = require('cookie-session');
var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var register = require('./routes/register');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/register', register);
app.use(cookieSession({
name: 'session',
keys: ['x', 'y']
}));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
这是我的login.js问题出在哪里
var express = require('express');
var router = express.Router();
var mongod = require('simple-mongod');
var UserManagement = require('user-management');
/* GET home page. */
router.get('/', function(req, res, next)
{
res.render('login');
});
router.post('/', function(req, res, next)
{
var users = new UserManagement();
users.load(function(err)
{
users.authenticateUser(req.body.login, req.body.pass, function(err, result)
{
if (!result.userExists)
{
console.log('Invalid username');
}
else if (!result.passwordsMatch)
{
console.log('Invalid password');
}
else
{
req.session.login = req.body.login;
res.redirect('/users');
}
users.close();
});
});
});
module.exports = router;
您要先添加 /login
路由,然后再添加 cookie-session。这意味着,当您的请求到达您的自定义登录路由时,仍然没有会话。会话将添加到请求中,但会在稍后添加。
app.use(cookieSession({
name: 'session',
keys: ['x', 'y']
}));
app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/register', register);
您需要在要使用它的路由之前注册 Cookie 会话中间件。
我的问题是关于 cookie 会话的。所以我在尝试处理 cookie-session 后遇到了这个问题。我无法设置任何会话。问题是当我试图设置它时 req.session.login
而这个 login
是个问题但我不知道为什么
process.nextTick(function() { throw err; });
^
TypeError: Cannot set property 'login' of undefined
at C:\Users\syriu\WebstormProjects\pubg\routes\login.js:30:35
这是我的app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cookieSession = require('cookie-session');
var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var register = require('./routes/register');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/register', register);
app.use(cookieSession({
name: 'session',
keys: ['x', 'y']
}));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
这是我的login.js问题出在哪里
var express = require('express');
var router = express.Router();
var mongod = require('simple-mongod');
var UserManagement = require('user-management');
/* GET home page. */
router.get('/', function(req, res, next)
{
res.render('login');
});
router.post('/', function(req, res, next)
{
var users = new UserManagement();
users.load(function(err)
{
users.authenticateUser(req.body.login, req.body.pass, function(err, result)
{
if (!result.userExists)
{
console.log('Invalid username');
}
else if (!result.passwordsMatch)
{
console.log('Invalid password');
}
else
{
req.session.login = req.body.login;
res.redirect('/users');
}
users.close();
});
});
});
module.exports = router;
您要先添加 /login
路由,然后再添加 cookie-session。这意味着,当您的请求到达您的自定义登录路由时,仍然没有会话。会话将添加到请求中,但会在稍后添加。
app.use(cookieSession({
name: 'session',
keys: ['x', 'y']
}));
app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/register', register);
您需要在要使用它的路由之前注册 Cookie 会话中间件。