表达将参数传递给路由抛出 TypeError('Router.use() 需要中间件功能但得到了

express passing params to route throws TypeError('Router.use() requires middleware function but got a

我正在为我的第一个 Express 应用程序构建架构。它的一部分是使用护照的身份验证服务。我正在努力解决下面描述的这个恼人的问题。

在我的服务器 app.js 文件中我有

var routes = require('./router/index')(app, passport);

passport 是我配置了 'local-login' 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"
}

你知道我做错了什么吗?预先感谢您的帮助。

router.useapp.use 期望一个函数 (req, res, next) - 长度为 3 或 (err, req, res, next) - 长度为 4,并且正在获得一个函数 (passport) - 1

的长度

类似...

module.exports = function(passport) {

  return 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
  });

}

或...

module.exports = function(passport) {

  return function(req, res, next) {
    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
    })(req, res, next);
  }

}

就是你想要的。

问题是您在 /router/routes/route.signin.js 中的导出函数没有返回中间件函数或请求处理程序(或 任何东西,这就是为什么您得到 undefined).

所以在 /router/routes/route.signin.js 中试试这个:

var express = require('express');
var router = express.Router();

module.exports = function(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
                    })
    )

    return router; // <--- add this
}