express
express copied to clipboard
Need a clarification on router.param() usage.
I was using router.param() method to run middleware in all the routes which has ':department_id'. for example: In my app, the route will be app.use('/department/:department_id/class',departmentRoutes); In my departmentRoutes, child route will be router.get('/:id', ...controller);
I have use { mergeParams : true } to include all the params in the router to consider.. but when i use router.param(':department_id',
But the when we use router.param('id',
kindly clear me of the doubt that, will the router.param() consider the params from the parent or app.
Hi @kisshore04, can you post more clear code examples? Ideally a single file/code block which illustrates your setup in its entirety? These partial lines do not help anyone answer your question. If it turns out this is a bug or in need of better docs or something I will leave this open awaiting more information, but we do not provide technical support so if that is what you are looking for you might have better luck on StackOverflow or Reddit.
@kisshore04 have you tried router.param(‘department_id’)? Without the colon. https://expressjs.com/en/4x/api.html
@kisshore04 have you tried router.param(‘department_id’)? Without the colon. https://expressjs.com/en/4x/api.html
yeah i have always used router.param('department_id'); without the colon. I mistyped it in here.
Hi @kisshore04, can you post more clear code examples? Ideally a single file/code block which illustrates your setup in its entirety? These partial lines do not help anyone answer your question. If it turns out this is a bug or in need of better docs or something I will leave this open awaiting more information, but we do not provide technical support so if that is what you are looking for you might have better luck on StackOverflow or Reddit.
I apologize for the confusion in my previous message. Here is the revised version of the issue. I'm encountering an issue with the router.param
function in Express.js. I have a setup where I'm expecting a callback function (myCallback
) to be executed when there is a request to the route /api/department/:department_id/class/:id
. However, during testing, I found that myCallback
is not being invoked.
Here's the relevant code from app.js
:
const express = require("express")
const app = express()
app.use("/api/department/:department_id/class", classRouter);
app.use("/api/department/", departmentRouter);
And from class-router.js
:
const express = require("express")
const router = express.Router({mergeParams: true})
router.param("department_id", myCallback);
router.get("/:id", controllerFuntion)
Interestingly, in my department router where I have a similar setup, the middleware is executing successfully. The route to this is: /api/department/:department_id
.
const express = require("express")
const router = express.Router({mergeParams: true})
router.param("department_id", myCallback);
router.get("/:department_id", getDeptDetails)
I've used the {mergeParams: true}
option while creating the router and I have not used the colon (:
) when I call the router.param()
function.
Any help on why myCallback
is not running when the request is coming to the nested route would be greatly appreciated.
@kisshore04 as per the router.param documentation, the callback will only be triggered by route params defined within the router. Since department_id
in your class-router.js
is a param inherited through mergeParams: true
, it won't trigger the .param
callback.
In your other router, the department_id
route param is defined within the router, so it triggers the .param
callback.
@wesleytodd Would it be worth updating the documentation so the sentence before the linked one goes from
They are not inherited by mounted apps or routers.
to
They are not inherited by mounted apps or routers, nor are they triggered for route parameters inherited from parent routers.