Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda