抛出新类型错误('app.use() requires middleware functions');
throw new TypeError('app.use() requires middleware functions');
我正在学习有关 Nodejs 的在线课程,并试图在我的快速应用程序中实施基本身份验证。但是我在 app.use(new localStrategy(User.Authenticate()); 上遇到错误我已经尝试重新安装 'passport-local'。为什么这个错误一直存在?
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
passport = require('passport'),
User = require('./models/user'),
bodyParser = require('body-parser'),
localStrategy = require('passport-local'); ;
passportLocalMongoose = require('passport-local-mongoose');
mongoose.connect('mongodb://localhost/auth_DB');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('images'));
app.use(require('express-session')({
secret: 'pppppqpqsda dasdqw ksndfkjnzmmuawt8ikweabmdsfj a',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
// body...
res.render('home');
});
app.get('/secret', function (req, res) {
// body...
res.render('secret');
});
app.get('/register', function (req, res) {
// body...
res.render('signup');
});
app.post('/register', function (req, res) {
User.register(new User({username: req.body.username}), req.body.password, function(err, user){
if(err){
console.log(err);
return res.render('signup');
}
passport.authenticate('local')(req, res, function(){
res.redirect('/secret');
});
});
});
app.get('/login', function (req, res) {
// body...
res.render('signin');
});
app.post('/login', passport.authenticate('local',
{successRedirect: '/secret',
failureRedirect: '/login'} ),
function(req, res){});
app.listen(3000, function(){
console.log('Server Started!');
});
Package.json :-
{
"name": "test-auth",
"version": "1.0.0",
"description": "Testing the authentication",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rishabh & Colt",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"express-session": "^1.15.2",
"mongoose": "^4.9.7",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0"
}
}
User.js:-
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');
var userSchema = new mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', userSchema);
错误:-
C:\Users\Atom\Project\testAuth\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires middleware functions');
^
TypeError: app.use() requires middleware functions
at EventEmitter.use (C:\Users\Atom\Project\testAuth\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\Atom\Project\testAuth\app.js:37:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
您的代码有错别字。 localStrategy
不是 express 的中间件。它可以与 passport
.
一起使用
替换
app.use(new localStrategy(User.authenticate()));
与
passport.use(new localStrategy(User.authenticate()));
您似乎需要错误的模块
您需要这样的本地策略
LocalStrategy = require('passport-local');
但在文档中,您需要添加“.Strategy”
http://passportjs.org/docs/configure
LocalStrategy = require('passport-local').Strategy;
我正在学习有关 Nodejs 的在线课程,并试图在我的快速应用程序中实施基本身份验证。但是我在 app.use(new localStrategy(User.Authenticate()); 上遇到错误我已经尝试重新安装 'passport-local'。为什么这个错误一直存在?
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
passport = require('passport'),
User = require('./models/user'),
bodyParser = require('body-parser'),
localStrategy = require('passport-local'); ;
passportLocalMongoose = require('passport-local-mongoose');
mongoose.connect('mongodb://localhost/auth_DB');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('images'));
app.use(require('express-session')({
secret: 'pppppqpqsda dasdqw ksndfkjnzmmuawt8ikweabmdsfj a',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
// body...
res.render('home');
});
app.get('/secret', function (req, res) {
// body...
res.render('secret');
});
app.get('/register', function (req, res) {
// body...
res.render('signup');
});
app.post('/register', function (req, res) {
User.register(new User({username: req.body.username}), req.body.password, function(err, user){
if(err){
console.log(err);
return res.render('signup');
}
passport.authenticate('local')(req, res, function(){
res.redirect('/secret');
});
});
});
app.get('/login', function (req, res) {
// body...
res.render('signin');
});
app.post('/login', passport.authenticate('local',
{successRedirect: '/secret',
failureRedirect: '/login'} ),
function(req, res){});
app.listen(3000, function(){
console.log('Server Started!');
});
Package.json :-
{
"name": "test-auth",
"version": "1.0.0",
"description": "Testing the authentication",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rishabh & Colt",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"express-session": "^1.15.2",
"mongoose": "^4.9.7",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0"
}
}
User.js:-
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');
var userSchema = new mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', userSchema);
错误:-
C:\Users\Atom\Project\testAuth\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires middleware functions');
^
TypeError: app.use() requires middleware functions
at EventEmitter.use (C:\Users\Atom\Project\testAuth\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\Atom\Project\testAuth\app.js:37:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
您的代码有错别字。 localStrategy
不是 express 的中间件。它可以与 passport
.
替换
app.use(new localStrategy(User.authenticate()));
与
passport.use(new localStrategy(User.authenticate()));
您似乎需要错误的模块 您需要这样的本地策略
LocalStrategy = require('passport-local');
但在文档中,您需要添加“.Strategy”
http://passportjs.org/docs/configure
LocalStrategy = require('passport-local').Strategy;