以下代码在启动服务器时给出错误。该代码来自基于ndejs,express和mongodb的Coursera课程。我试图纠正错误,甚至复制了课程中给出的代码,但均无济于事。终端显示的错误是在Passport-local-mongoose模块的index.js中,使我相信在实现过程中我做错了什么。请帮助我解决这个问题。
启动服务器后,它将打印,
ent/home/sanket/VSCode/NodeJS/conFusionServer/node_modules/passport-local-mongoose/index.js:125
.then(({ user }) => {
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/sanket/VSCode/NodeJS/conFusionServer/models/users.js:3:29)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/sanket/VSCode/NodeJS/conFusionServer/authenticate.js:3:12)
at Module._compile (module.js:410:26)
以下是与此错误相关的文件,
models / users.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var User = new Schema({
admin: {
type: Boolean,
default: false
}
});
User.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', User);
routes / users.js
var express = require('express');
const bodyParser = require('body-parser');
var User = require('../models/users');
var passport = require('passport');
var router = express.Router();
router.use(bodyParser.json());
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.post('/signup', (req, res, next) => {
User.register(new User({username: req.body.username}),
req.body.password, (err, user) => {
if(err) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.json({err: err});
}
else {
passport.authenticate('local')(req, res, () => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json({success: true, status: 'Registration Successful!'});
});
}
});
});
router.post('/login', passport.authenticate('local'), (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json({success: true, status: 'You are successfully logged in!'});
});
router.get('/logout', (req, res, next) => {
if (req.session) {
req.session.destroy();
res.clearCookie('session-id');
res.redirect('/');
}
else {
var err = new Error('You are not logged in!');
err.status = 403;
next(err);
}
});
module.exports = router;
authenticate.js
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/users');
exports.local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var FileStore = require('session-file-store')(session);
var passport = require('passport');
var authenticate = require('./authenticate');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var dishRouter = require('./routes/dishRouter');
var promoRouter = require('./routes/promoRouter');
var leaderRouter = require('./routes/leaderRouter');
const mongoose = require('mongoose');
const Dishes = require('./models/dishes');
const url = 'mongodb://localhost:27017/conFusion';
const connect = mongoose.connect(url);
connect.then((db) => {
console.log('Connected correctly to the server');
}, (err) => {
console.log(err);
});
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({
name: 'session-id',
secret: '12345-67890-09876-54321',
saveUninitialized: false,
resave: false,
store: new FileStore()
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/', indexRouter);
app.use('/users', usersRouter);
function auth (req, res, next) {
if (!req.user) {
var err = new Error('You are not authenticated!');
err.status = 403;
return next(err);
}
else {
next();
}
}
app.use(auth);
app.use(express.static(path.join(__dirname, 'public')));
app.use('/dishes', dishRouter);
app.use('/promotions', promoRouter);
app.use('/leaders', leaderRouter);
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
这是课程Server-side Development with NodeJS, Express and MongoDB
的链接答案 0 :(得分:1)
将此行替换为authenticate.js文件
exports.local = passport.use(new LocalStrategy(User.authenticate()));
使用
passport.use(new LocalStrategy(User.authenticate()));
应该可以。
这是我的话题 https://www.coursera.org/learn/server-side-nodejs/discussions/weeks/3/threads/zVHS6leUSwOR0upXlOsD0Q
如果它不起作用,请告知。