【发布时间】:2015-01-27 07:25:25
【问题描述】:
我正在为我的第一个 express 应用程序构建一个架构。其中之一是使用护照的身份验证服务。我正在努力解决下面描述的这个烦人的问题。
在我的服务器 app.js 我有文件
var routes = require('./router/index')(app, passport);
passport 是我的护照对象,配置了“本地登录”LocalStrategy。我想将此对象传递给我的路由器索引,然后传递给我的登录路由。
在我的/router/index.js 我有
module.exports = function (app, passport) {
app.use('/signin', require('./routes/route.signin')(passport));
};
在/router/routes/route.signin.js 我有
var express = require('express');
var router = express.Router();
module.exports = function(passport) {
console.log(passport)
router.post('/', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
})
)
}
console.log(passport) 记录了我的护照对象,但是这段代码给了我一个错误:
/myapp/server/node_modules/express/lib/router/index.js:438
throw new TypeError('Router.use() requires middleware function but got a
^
TypeError: Router.use() requires middleware function but got a undefined
at Function.<anonymous> (/myapp/server/node_modules/express/lib/router/index.js:438:13)
at Array.forEach (native)
at Function.use (/myapp/server/node_modules/express/lib/router/index.js:436:13)
at EventEmitter.<anonymous> (/myapp/server/node_modules/express/lib/application.js:187:21)
at Array.forEach (native)
at EventEmitter.use (/myapp/server/node_modules/express/lib/application.js:184:7)
at module.exports (/myapp/server/router/index.js:12:9)
at Object.<anonymous> (/myapp/server/app.js:28:39)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
这是我来自 package.json 文件的依赖项:
"dependencies": {
"bcrypt": "^0.8.0",
"body-parser": "~1.10.1",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.3.3",
"express": "~4.10.6",
"express-session": "^1.10.1",
"mongoose": "^3.8.21",
"morgan": "~1.5.1",
"passport": "^0.2.1",
"passport-local": "^1.0.0"
}
你有什么想法我做错了什么吗?提前感谢您的帮助。
【问题讨论】:
标签: node.js express passport.js