The home router "/" in express-generator is being overwritten somewhere, or it's just not working. Why?

65 views Asked by At

I am trying to connect mongodb with express-generator. My default home router "/" is in index.js, but it's not working. It's not invoked. It only works if I change the router to something else.

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  console.log('here')
  const collection = req.app.locals.collection;
  collection.find({})
  .toArray()
  .then(data => res.json(data))
});

module.exports = router;

This is my index.js.

And in my app.js, I have this:

var indexRouter = require('./routes/index');

MongoClient.connect(uri, (err, client)=>{
    if(err) console.log('we have an error: ', err)
    const db = client.db('MovieGoers');
    const collection = db.collection('users')
    app.locals.collection = collection;
})

app.use('/', indexRouter);

It's not working. The function is not invoked. If I change the router in index.js to "/movies" or anything else, it works in that router, but the default home router "/" is not working. Nothing is invoked. I keep seeing the "welcome to express" header from the index.html.

Is something overwriting this router?

0

There are 0 answers