【问题标题】:express passing params to route throws TypeError('Router.use() requires middleware function but got aexpress 将参数传递给路由会抛出 TypeError('Router.use() 需要中间件函数但得到了一个
【发布时间】: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


    【解决方案1】:

    问题是您在/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
    }
    

    【讨论】:

    • 如此简单:) 你的回答解决了我的问题。谢谢大家
    【解决方案2】:

    router.use 或 app.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);
      }
    
    }
    

    是你想要的。

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 2019-12-23
      • 2015-07-08
      • 2015-02-12
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多