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

217
Views
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 answers
Answer question

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 Report

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 Report
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!