我正在使用node.js的护照模块,我收到以下错误:
TypeError: undefined is not a function
at module.exports (C:\cygwin64\home\Home\gethweb\routes\index.js:21:33)
at Layer.handle [as handle_request] (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:312:13)
at C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:330:12)
at next (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:271:10)
at C:\cygwin64\home\Home\gethweb\node_modules\connect-flash\lib\flash.js:21:5
at Layer.handle [as handle_request] (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:312:13)
at C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:330:12)
at next (C:\cygwin64\home\Home\gethweb\node_modules\express\lib\router\index.js:271:10)
at SessionStrategy.strategy.pass (C:\cygwin64\home\Home\gethweb\node_modules\passport\lib\middleware\authenticate.js:318:9)
at SessionStrategy.authenticate (C:\cygwin64\home\Home\gethweb\node_modules\passport\lib\strategies\session.js:67:10)
at attempt (C:\cygwin64\home\Home\gethweb\node_modules\passport\lib\middleware\authenticate.js:341:16)
at authenticate (C:\cygwin64\home\Home\gethweb\node_modules\passport\lib\middleware\authenticate.js:342:7)
以下是我的index.js文件的相关部分:
var express = require('express');
var passport = require('passport');
router = express.Router();
module.exports = function(passport) {
router.get('/', function(req, res) {
res.render('index', { message: req.flash('message') });
});
router.post('/login', passport.authenticate('login', {
successRedirect: '/home',
failureRedirect: '/',
failureFlash : true
}));
router.get('/signup', function(req, res) {
res.render('register',{message: req.flash('message')});
});
router.post('/signup', passport.authenticate('signup', {
successRedirect : '/home',
failureRedirect : '/signup',
failureFlash : 'true'
}));
return router;
};
编辑:
init.js:
var login = require('./login.js');
var signup = require('./signup.js');
var User = require('../models/user');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
console.log('serializing user: '); console.log(user);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('deserializing user: ', user);
done(err, user);
});
});
login(passport);
signup(passport);
}
和signup.js:
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
var bCrypt = require('bCrypt-nodejs');
module.exports = function(passport) {
passport.use('signup', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
findOrCreateUser = function() {
User.findOne({ 'username' : username }, function(err, user) {
if (err) {
console.log('Error in SignUp: '+err);
return done(err);
}
if (user) {
console.log('User already exists with username' + username);
return done(null, false, req.flash('message', 'User Already Exists'));
} else {
var newUser = new User();
newUser.username = username;
newUser.password = createHash(password);
newUser.email = req.param('email');
newUser.firstName = req.param('firstName');
newUser.lastName = req.param('lastName');
newUser.save(function(err) {
if (err) {
console.log('Error in Saving user: ' + err);
throw err;
};
console.log('User Registration successful');
return done(null, newUser);
});
}
});
};
process.nextTick(findOrCreateUser);
})
);
var createHash = function(password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
}
}
有谁能告诉我导致错误的原因是什么?非常感谢。