我正在使用Mozilla’s client-sessions在我的express.js应用上实现会话,我需要一些帮助。我在我的router.js和app.js中的中间件中有post和gest请求,运行时出现此错误:
ReferenceError:未定义requireLogin
// index.js(路由器)
var express = require('express')
, router = express.Router()
, user = require('../public/js/models/test.js');
router.get('/profile-header', requireLogin, function (req, res, html) {
console.log(req.session);
res.render('templates/profile-header.jade', { title: 'Default' })
});
router.get('/sign-in', function (req, res, html) {
res.render('templates/sign-in.jade', { title: 'Sign In' })
});
router.get('/blog-home', requireLogin, function (req, res, html) {
res.render('templates/blog-home.jade', { title: 'Blog' })
});
router.get('/logout', function(req, res) {
req.session.reset();
res.redirect('/');
});
router.post('/profile-header', function (req, res, html) {
user.login(req.body, function (e, o) {
if (o) {
//add session
req.session.user = o;
res.redirect('/profile-header');
} else {
res.render('templates/sign-in.jade', { error: 'Invalid email or password.' });
}
});
});
module.exports = router;
// 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 stylus = require('stylus');
var bcrypt = require('bcrypt');
var should = require('should');
var session = require('client-sessions');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
function requireLogin (req, res, next) {
if (!req.user) {
console.log("pd");
res.redirect('/sign-in');
} else {
next();
}
};
app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(stylus.middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use('/', index);
app.use('/users', users);
app.use(function(req, res, next) {
if (req.session && req.session.user) {
user.exist(req.session.username, function (e, o) {
if (user) {
req.user = user;
delete req.user.user_password; // delete the password from the session
req.session.user = user; //refresh the session value
console.log(req.session);
res.locals.user = user;
}
// finishing processing the middleware and run the route
next();
});
} else {
next();
}
});
// listener
app.listen(process.env.PORT || 3000, function () {
console.log('Listening on http://localhost:' + (process.env.PORT || 3000))
})
module.exports = app;
谢谢你的帮助!
答案 0 :(得分:2)
问题在于requireLogin
中定义了app.js
,但index.js
中未定义requireLogin
。
我建议在外部服务中移动// auth.service.js
exports.requireLogin = function (req, res, next) { ... };
并在需要时导入/需要它:
index.js
然后在var Auth = require('./auth.service');
....
router.get('/profile-header', Auth.requireLogin, function (req, res, html) {
console.log(req.session);
res.render('templates/profile-header.jade', { title: 'Default' })
});
中添加:
@Enumerated(EnumType.STRING)
VehicleType type