我正在尝试将Passport js本地策略添加到我的nodejs应用程序。但是每次我将此文件添加到app.js时,都会收到类似这样的错误。 在将password.js添加到app.js文件之前,该代码运行良好。
我不熟悉使用护照认证。我不知道问题出在哪里
C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\adity\Desktop\thinkster\medium-api\app.js:28:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[nodemon] app crashed - waiting for file changes before starting...
这些是我的密码
config / passport.js
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const User = require('../models/User');
mongoose.model('User');
passport.use(new LocalStrategy({
username: 'user[email]',
password: 'user[password'
},function(email, password, done) {
User.findOne({email: email}).then(function (user) {
if(!user || !user.validPassword(password)){
return done(null, false, {errors: {'email of password': 'is invalid'}});
}
return done(null, user);
}).catch(done);
}
));
app.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const session = require('express-session');
const app = express();
const conn = mongoose.connect('mongodb://localhost:27017/medium', {
useNewUrlParser: true,
useCreateIndex: true
});
app.use(session({secret: 'medium',
cookie: {maxAge: 6000}, resave: false,
saveUninitialized:false}));
app.use(passport.initialize());
if (!conn) {
console.log('Error with database');
} else {
console.log('Database Connected');
}
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(require('./config/passport'));
app.use(require('./routes'));
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server started on ${PORT}`);
});
答案 0 :(得分:0)
尝试将导出添加到您的配置文件中
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const User = require('../models/User');
mongoose.model('User');
module.exports = passport.use(new LocalStrategy({
username: 'user[email]',
password: 'user[password'
},function(email, password, done) {
User.findOne({email: email}).then(function (user) {
if(!user || !user.validPassword(password)){
return done(null, false, {errors: {'email of password': 'is invalid'}});
}
return done(null, user);
}).catch(done);
}
));
我还没有测试过,只是猜测,所以让我知道这是否有效;)