我试图使用localdb运行服务器,在服务器端,我在这里有这个代码...登录文件在主目录中。我收到以下错误:如果有人可以帮忙...
Error C:\Users\user\Desktop\projectApp\node_modules\express\lib\router\route.js:196
throw new Error(msg);
^
Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (C:\Users\user\Desktop\projectApp\node_modules\express\lib\router\route.js:196:15)
at EventEmitter.app.(anonymous function) [as get] (C:\Users\user\Desktop\projectApp\node_modules\express\lib\application.js:481:19)
at Object.<anonymous> (C:\Users\user\Desktop\projectApp\server.js:7:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
Server.js
var express = require('express');
var login = require('./login');
express()
.set('view engine', 'ejs')
.use(express.static('./public'))
.use(login.routes)
.get('*',login.required, function (req, res) {
res.render('index',{
user: login.safe(req.user)
});
}).listen(3000);
**Login.js**
var passport = require('passport');
var LocalStrategy = require('passport-local');
var LocallyDB = require('locallydb');
var db = new LocallyDB('./.data');
var users = db.collection('users');
var crypto = require('crypto');
function hash (password) {
return crypto.createHash('sha512').update(password).digest('hex');
}
passport.use(new LocalStrategy(function (username, password, done) {
var user = users.where({ username: username, passwordHash: hash(password) }).items[0];
if (user) {
done(null, user);
} else {
done(null, false);
}
}));
passport.serializeUser(function (user, done) {
done(null, user.cid);
});
passport.deserializeUser(function (cid, done) {
done(null, users.get(cid));
});
var router = require('express').Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true })); // Login Page
router.use(bodyParser.json()); // API
router.use(require('cookie-parser')());
router.use(require('express-session')({
secret: 'p7r6uktdhmcgvho8o6e5ysrhxmcgjfkot7r6elu5dtjt7lirfyj',
resave: false,
saveUninitialized: true
}));
router.use(passport.initialize());
router.use(passport.session());
router.get('/login', function (req, res) {
res.render('login');
});
router.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
}));
exports.routes = router;
enter code here
enter code here
enter code here