title: Node throws TypeError: Cannot read property 'indexOf' of undefined
body: <p>Not certain, but think I have something that SHOULD work, but it doesn't, and the error I get is quite unhelpful.</p>
  
  <p>So, trying to send the passport object to one of the routes, which I understand should be just to set</p>
  
  <pre><code>var users = require('./routes/users')(app, passport);
  </code></pre>
  
  <p>Which throws a fault on startup</p>
  
  <pre><code>&gt; /Users/bengtbjorkberg/.nvm/versions/node/v0.12.0/bin/node app.js Test
  &gt; something 2
  &gt; /Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:130
  &gt; var search = 1 + req.url.indexOf('?');
  &gt;                           ^ TypeError: Cannot read property 'indexOf' of undefined
  &gt;     at Function.proto.handle (/Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:130:27)
  &gt;     at router (/Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:35:12)
  &gt;     at Object.&lt;anonymous&gt; (/Users/bengtbjorkberg/WebstormProjects/goMinute/app.js:41:38)
  &gt;     at Module._compile (module.js:460:26)
  &gt;     at Object.Module._extensions..js (module.js:478:10)
  &gt;     at Module.load (module.js:355:32)
  &gt;     at Function.Module._load (module.js:310:12)
  &gt;     at Function.Module.runMain (module.js:501:10)
  &gt;     at startup (node.js:129:16)
  &gt;     at node.js:814:3
  &gt; 
  &gt; Process finished with exit code 1
  </code></pre>
  
  <p>If I remove app and passport, the problem disappears magically. the routs in ./routes/users does not use any of them yet.</p>
  
  <p>I have added some of the code (removed a bit of passport configuration for good form)</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
  var stylus = require('stylus');
  var nib = require('nib');
  var morgan = require('morgan');
  
  //  express session and passport
  var session = require('express-session');
  var passport = require('passport');
  var flash    = require('connect-flash');
  
  
  
  
  var app = express();
  function compile(str, path) {
      return stylus(str)
          .set('filename', path)
          .use(nib())
  }
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  var accesslevels = require('./config/accesslevels.js');
  console.log("Test something " + accesslevels.userRoles.user);
  // required for passport
  app.use(session({ secret: '' + 'asifIwouldtellstackoverflowright', resave: true, saveUninitialized: true })); // session secret
  app.use(passport.initialize());
  app.use(passport.session()); // persistent login sessions
  app.use(flash()); // use connect-flash for flash messages stored in session
  
  //Routes after passports
  var routes = require('./routes/index');
  var users = require('./routes/users')(app, passport);
  var loginsrv = require('./routes/login');
  var api = require('./routes/api');
  var partials = require('./routes/partials');
  
  var auth = function(req, res, next) {
      if (!req.isAuthenticated())
          res.send(401);
      else
          next();
      };
  //See more at: https://vickev.com/#!/article/authentication-in-single-page-applications-node-js-passportjs-angularjs
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(__dirname + '/public/favicon.ico'));
  app.use(logger('dev'));
  app.use(bodyParser.json()); // get information from html forms
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(cookieParser());
  app.use(require('stylus').middleware(path.join(__dirname, 'public')));
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', routes);
  app.use('/users', users);
  app.use('/partials', partials);
  app.use('/loginsrv', loginsrv);
  
  //app.use(accesslevels());
  
  //console.log(accesslevels);
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
  });
  
  // error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
      app.use(function(err, req, res, next) {
          res.status(err.status || 500);
          res.render('error', {
              message: err.message,
              error: err
          });
      });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: {}
      });
  });
  
  
  module.exports = app;
  app.listen(30010);
  </code></pre>
  
  <p>EDIT: Added routes/users.js</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  /* GET users listing. */
  router.get('/', function(req, res, next) {
    res.send('respond with a resource');
  });
  
  /*
  // route to test if the user is logged in or not
  router.get('/loggedin', function(req, res) {
      res.send(req.isAuthenticated() ? req.user : '0');
  });
  
  router.post('/login', passport.authenticate('local'), function(req, res) {
      res.send(req.user);
  });
  
  router.post('/logout', function(req, res){
      req.logOut();
      res.send(200);
  });
  */
  
  module.exports = router;
  </code></pre>
  
  <p>In the above code I have changed the original <code>router.get('/', function(req, res, next)</code> with     <code>router.get('/', function(app, passport, req, res, next)</code> which resolves the problem. </p>