Friday 21 April 2017

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.



via Jay

No comments:

Post a Comment