title: nodeJS/Express TypeError : Cannot read property 'method' of undefined
body: <p>Hello I am getting an error when trying to start my server. This is what it says : </p>
  
  <p>D:\Other\Projects\Code\Powershell\shopping-cart\node_modules\express\lib\router\index.js:139
    debug('dispatching %s %s', req.method, req.url);
                                   ^</p>
  
  <pre><code>TypeError: Cannot read property 'method' of undefined
      at Function.handle (D:\Other\Projects\Code\Powershell\shopping-cart\node_modules\express\lib\router\index.js:139:34)
      at router (D:\Other\Projects\Code\Powershell\shopping-cart\node_modules\express\lib\router\index.js:47:12)
      at Object.&lt;anonymous&gt; (D:\Other\Projects\Code\Powershell\shopping-cart\app.js:7:39)
      at Module._compile (internal/modules/cjs/loader.js:1158:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
      at Module.load (internal/modules/cjs/loader.js:1002:32)
      at Function.Module._load (internal/modules/cjs/loader.js:901:14)
      at Module.require (internal/modules/cjs/loader.js:1044:19)
      at require (internal/modules/cjs/helpers.js:77:18)
      at Object.&lt;anonymous&gt; (D:\Other\Projects\Code\Powershell\shopping-cart\bin\www:7:11)
      at Module._compile (internal/modules/cjs/loader.js:1158:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
      at Module.load (internal/modules/cjs/loader.js:1002:32)
      at Function.Module._load (internal/modules/cjs/loader.js:901:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
      at internal/main/run_main_module.js:18:47
  </code></pre>
  
  <p>My app.js file :  </p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  
  var routes = require('./routes/index')(passport);
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  var expressHbs = require('express-handlebars');
  var mongoose =require('mongoose');
  var app = express();
  var session = require('express-session');
  var passport = require('passport');
  var flash = require('connect-flash');
  var bodyParser = require('body-parser');
  
  mongoose.connect('mongodb://localhost:27017/shopping', {useNewUrlParser: true, useUnifiedTopology: true});
  mongoose.connection.on('error', err =&gt; {
    throw 'failed to connect to MongoDB';
  });
  require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');
  
  
  
  // view engine setup
  app.engine('.hbs', expressHbs({defaultLayout: 'layout', extname:'.hbs'}))
  app.set('view engine', '.hbs');
  
  app.use(bodyParser.urlencoded({extended: false}));
  app.use(bodyParser.json());
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(session({secret: 'mysupersecret', resave: false, saveUninitialized: false}));
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(express.static(path.join(__dirname, 'public')));
  
  
  
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    next(createError(404));
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  </code></pre>
  
  <p>And my index.js file : </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');
  var csrf = require('csurf');
  //var passport = require('D:/Other/Projects/Code/Powershell/shopping-cart/config/passport.js');
  var passport = require ('passport');
  var csrfProtection = csrf();
  router.use(csrfProtection);
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    Product.find(function(err, docs) {
      var productChunks = [];
      var chunkSize = 3;
      for (var i = 0; i &lt; docs.length; i += chunkSize) {
             productChunks.push(docs.slice(i, i + chunkSize));
      }
      res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
    });
  });
  
  router.get('/user/signup', function (req, res, next) {
     res.render('user/signup', {csrfToken: req.csrfToken()});
  });
  
  router.post('/user/signup', passport.authenticate('local.signup', {
    successRedirect: '/user/profile',
    failureRedirect: '/user/signup',
    failureFlash: true
  }));
  
  router.get('/user/profile', function(req, res, next) {
    res.render('user/profile');
  });
  
  module.exports = router;
  </code></pre>
  
  <p>Any hints on what could be the issue? I checked the lines that are being mentioned in the thrown error and can not find anything that is broken, and I have also checked other questions on this error, none that helped me. Any help is appreciated , thanks!</p>