Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!