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

214
Vistas
Express js flow behavior

I need to understand how flow works in an Express app using Routes, I have These Routes

app.use(require('./routes/reportsRouter'));
app.use(require('./routes/crewsRouter'));
app.use(require('./routes/api'));
app.use(require('./routes/filesRouter'));

Now in ./routes/crewsRouter I have th following Code

var express = require('express');
var router = express.Router(); 
router.use(function(req, res, next) {

  var url = req.url;
  //..... Edit URL if Contains // empty parm 
  // crews//today; correct Url crews/all/today
  // this give me a list of all jobs for all crews for today. 
  console.log("CrewsRouter: ", req.method + ".( " + url + " )");
  next(); 
});
router.get('/crews', function(req, res) {
    if (!req.params.key) { next(); }
    res.render('crewsView',{
        pageTitle:'All-Crews',
        pageID:'crews', 
        crewInfo: {"aka": "all"},
        reqOptions: ''
    });

});
router.get('/crews/:leadId?/:options?', function(req, res) {....}
module.exports = router;

and in reportsRouter.js

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

router.use(function(req, res, next) {

    // log each request to the console
    console.log("ReportsRouter: ", req.method + ".( " + req.url + " )");
    // continue doing what we were doing and go to the route
    next(); 
});

router.get('/reports', function(req, res) {
    //var data = req.app.get('appData')

    res.render('reportsView',{
        pageTitle:'Reports',
        pageID:'reports'        
    });
});
module.exports = router;

The behavior I'm having is no matter what route I request both of the route.use is running. Is this normal and what can i do to stop this behavior.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

let crewsRouter = require('routes/crewsRouter');
...

app.use('/crews', crewsRouter);
app.use('/reports', reportsRouter);

# crews

...
router.get('/', function(req, res) {
    ... # this used to be your '/crews' handler
}

# reports

...
router.get('/', function(req, res) {
    ... # this used to be your '/reports' handler
}
about 4 years ago · Santiago Trujillo Denunciar

0

You should probably be doing something like this:

app.use('/reports', require('./routes/reportsRouter'));
app.use('/crews', require('./routes/crewsRouter'));
app.use('/api', require('./routes/api'));
app.use('/files', require('./routes/filesRouter'));

And then in your reportsRouter:

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

router.use(function(req, res, next) {

    // log each request to the console
    console.log("ReportsRouter: ", req.method + ".( " + req.url + " )");
    // continue doing what we were doing and go to the route
    next(); 
});

router.get('/', function(req, res) {
    //var data = req.app.get('appData')

    res.render('reportsView',{
        pageTitle:'Reports',
        pageID:'reports'        
    });
});
module.exports = router;

And your crewsRouter:

var express = require('express');
var router = express.Router(); 
router.use(function(req, res, next) {

  var url = req.url;
  //..... Edit URL if Contains // empty parm 
  // crews//today; correct Url crews/all/today
  // this give me a list of all jobs for all crews for today. 
  console.log("CrewsRouter: ", req.method + ".( " + url + " )");
  next(); 
});
router.get('/', function(req, res) {
    if (!req.params.key) { return next(); }
    res.render('crewsView',{
        pageTitle:'All-Crews',
        pageID:'crews', 
        crewInfo: {"aka": "all"},
        reqOptions: ''
    });
});
router.get('/:leadId?/:options?', function(req, res) {....});
module.exports = router;
about 4 years ago · Santiago Trujillo Denunciar
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