Passed in different variables through middle-wear router, but always comes out the same result by different URLs

Express version: 4.17.1


I am planning to access following two URLs, and trying to process 2 URLs with one log controller through a log router

  • /buy/:id/log
  • /sale/:id/log

Previously, the log controller was linked with ‘buy’ and ‘sell’ router files, and works fine.
After adding log router file as a middle-wear, result always shows same result, no matter which URL was used.

I dont know if it is Express error, or JS error


Code concept:

  1. route(‘/buy’) or route(‘/sell’)
  2. route(‘/:id/log’) and pass in BuyModel / SellModel
  3. Process request

Simplified code:

In index.js

app.use('/buy', require('/routes/buy.js'))
app.use('/sell', require('/routes/sell.js'))

In ‘buy.js’ router file:

router.use('/:id/log', require('./log.js')(BuyModel))

In ‘sell.js’ router file:

router.use('/:id/log', require('./log.js')(SellModel))

In ‘log.js’ router file:

module.exports = function(model){
  router.route('/')
    .get(fetchLogs(model))
  return router
}

In ‘log’ controller file

exports.fetchLogs = model => (req, res) => {
  console.log ('model', model)
  console.log ('base url', req.baseUrl)
}

Base url is correct, follows the right path, while result always show BuyModel
Suspecting if due to buy.js and sell.js are pre-loaded and model param was replaced.

1 possible answer(s) on “Passed in different variables through middle-wear router, but always comes out the same result by different URLs

  1. Hi @ramos-ph ,
    Thank you very much for your code, and it works like a charm!
    I did pass model object in req to temporally bypass the issue, while just not feeling right.
    And now I understand why dougwilson said the issue was created by a router.