Not certain, but think I have something that SHOULD work, but it doesn't, and the error I get is quite unhelpful.
So, trying to send the passport object to one of the routes, which I understand should be just to set
var users = require('./routes/users')(app, passport);
Which throws a fault on startup
> /Users/bengtbjorkberg/.nvm/versions/node/v0.12.0/bin/node app.js Test
> something 2
> /Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:130
> var search = 1 + req.url.indexOf('?');
> ^ TypeError: Cannot read property 'indexOf' of undefined
> at Function.proto.handle (/Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:130:27)
> at router (/Users/bengtbjorkberg/WebstormProjects/goMinute/node_modules/express/lib/router/index.js:35:12)
> at Object.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/goMinute/app.js:41:38)
> at Module._compile (module.js:460:26)
> at Object.Module._extensions..js (module.js:478:10)
> at Module.load (module.js:355:32)
> at Function.Module._load (module.js:310:12)
> at Function.Module.runMain (module.js:501:10)
> at startup (node.js:129:16)
> at node.js:814:3
>
> Process finished with exit code 1
If I remove app and passport, the problem disappears magically. the routs in ./routes/users does not use any of them yet.
I have added some of the code (removed a bit of passport configuration for good form)
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);
EDIT: Added routes/users.js
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;
In the above code I have changed the original router.get('/', function(req, res, next)
with router.get('/', function(app, passport, req, res, next)
which resolves the problem.
Just in case, if you have any HTTP verb routes app.get('/' ...) been used after app.use(..) then you will get this error in Express4x.
Ref here
I am not certain I understand why, but the trick was to changed the original router.get('/', function(req, res, next)
with router.get('/', function(app, passport, req, res, next)
which resolves the problem. Not certain why express requires a function that does not use the passed object to have passport and app, but it does it seems.
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(app,passport, 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With