expressing passing parameters to start the throws TypeError ('Router.use () requires a middleware function but got a

advertisements

I'm building an architecture for my first express application. One of its part is the authentication service using passport. I'm struggling with this annoying problem described below.

In my server app.js file I have

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

passport is my passport object configured with 'local-login' LocalStrategy. I want to pass this object to my router index and then to my signin route.

In my /router/index.js I have

module.exports = function (app, passport) {
    app.use('/signin', require('./routes/route.signin')(passport));
};

and in /router/routes/route.signin.js I have

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

The console.log(passport) logs my passport object, however this code gives me an error:

/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)

Here are my dependencies from package.json file:

"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"
}

Do you have any ideas what am I doing wrong? Thanks in advance for your help.


The problem is that your exported function in /router/routes/route.signin.js is not returning a middleware function or request handler (or anything for that matter, which is why you get undefined).

So try this instead in /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
}