Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda