How to access to the previous URL parameters from router
Check https://expressjs.com/en/4x/api.html#express.router
In short:
var router = express.Router({mergeParams: true})
If you have a GET
request in your API
set up like:
app.get('/projects/:project_id/context_items/:ci_id', function(req, res) {})
You can acess both project_id
and ci_id
using req.param
.
However, if you have a parameter middleware like:
app.param('[project_id', 'ci_id']', (req, res, next, value)=>{ next();});
You can bind anything you want to the request, e.g. req.value = value
Since the middleware runs before every route that matches either the project_id
or the ci_id
parameter, req.value
value will change depending on your request.
So if your server gets this request: GET /project/12/context_items/24
and your API
has a route set up that logs the value of req.param
:
app.get('/projects/:project_id/context_items/:ci_id', function(req, res) { console.log(req.value) });
It will first log 12, then 24.