title: Node Express API routing, Id of parent resource not available in req.params
body: <p>My problem is that I don't get the id of parent resource in req.params in routes of subresources. Come with me ... I'll show you ...</p>
  
  <p>Here's the relevant parts in my code:</p>
  
  <pre><code> - routes/
      - index.js
      - countries.js
      - cities.js
   - controllers/
      - cities.js
  </code></pre>
  
  <p>index.js</p>
  
  <pre><code>let router = require('express').Router();
  
  router.use('/countries', require('./countries'));
  router.use('/countries/:countryId/cities', require('./cities'));
  
  module.exports = router;
  </code></pre>
  
  <p>routes/countries.js</p>
  
  <pre><code>const router = require('express').Router();
  
  const countries = require('../controllers/countries');
  
  router.post('/', countries.create);
  
  router.get('/', countries.readAll);
  
  router.get('/:_id', countries.readById);
  
  router.patch('/:_id', countries.updateById);
  
  router.delete('/:_id', countries.deleteById);
  
  module.exports = router;
  </code></pre>
  
  <p>routes/cities.js</p>
  
  <pre><code>const router = require('express').Router();
  
  const cities = require('../controllers/cities');
  
  router.get('/', cities.readAll);
  
  router.get('/:_id', cities.readById);
  
  module.exports = router;
  </code></pre>
  
  <p>controllers/cities.js</p>
  
  <pre><code>module.exports.readAll = async function (req, res, next)
  {
      return res.json(req.params);
  };
  
  module.exports.readById = async function (req, res, next)
  {
      return res.json(req.params);
  };
  </code></pre>
  
  <p>Now, the problem is as follows:</p>
  
  <p>If I call the following API:</p>
  
  <p>GET countries/1/cities</p>
  
  <p>The result is {} 
  When it should actually send me back  {"countryId":"1"}</p>
  
  <p>Also, when I call the following API:</p>
  
  <p>GET countries/1/cities/2</p>
  
  <p>The result is {"_id": "2"}
  When it should actually send me back {"countryId":"1", "_id": "2"}</p>
  
  <p>So the problem is for some reason, the id of the parent resource (countryId) is not accessible in the controller of the child resource.</p>
  
  <p>Please advise</p>