question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for multiple parameter names in router.param()

See original GitHub issue

Hi,

I just found out about router.param() and immediately started using it in my code. It makes checks based on parameters so darn clean 😃 Really good job on the inventor of that idea!

While using it i found myself wondering with a url like: “/:year/:month/:day”. How can you make a router.param(…) function that is called when exactly those 3 arguments have been provided in the url? Perhaps it is possible, but i haven’t seen a way in the documentation.

Since router.param() works on parameter names, i would suggest a feature like the following to allow for an implementation of the above to be possible in router.param().

router.param(['year', 'month', 'day'], (req, res, next, obj) => {
   obj.year; // contain the year value
   obj.month; // contain the month value
   obj.day; // contain the day value
});

I would say that the first array in which the params are provided as string must be the exact order in which they would occur in the app.get(‘/:year/:month/:day’ …) function;

Is there any technical (or practical) reason why a feature like this wouldn’t be possible? What are your thoughts on this?

Best regards, Mark

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
kevinkassimocommented, Aug 3, 2017

Having briefly looked into the source code of router.param() (which is proto.param() in router/index.js), I noticed that all current router.param() did is push the callback on the router.params[paramName] array, and does nothing else. router.handle(), a not documented method, does the actual work. It calls another internal method router.process_params(), that actually invokes the pushed callbacks on each parameter one by one, sequentially without repeat. From index.js:

proto.process_params = function process_params(layer, called, req, res, done) {  
    var params = this.params;
    //...
    function param() {
        //...
        key = keys[i++];
        name = key.name;
        //...
        paramCallbacks = params[name];
        //...
        paramCallback();
    }
    function paramCallback(err) {
        var fn = paramCallbacks[paramIndex++];
        //...
        if (!fn) return param();
        
        try {
            fn(req, res, paramCallback, paramVal, key.name);
        } catch (e) {
            paramCallback(e);
        }
    }

    param();
}    

I believe to add the feature you asked may require changing these structures, and I don’t feel of that much necessity if you can just give the job to a normal app.use() like methods.

0reactions
oktapodiacommented, Dec 7, 2020

It would be good to have the router.param() accepting multiple middlewares, that will permit us to split our middleware

example:

router.param('userId', 
	(req: Request, res: Response, next: NextFunction) => {
	  // Common middleware validating the data
	},
	(req: Request, res: Response, next: NextFunction) => {
	  // Middleware to assign the data, etc...
	}
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple params with React Router - Stack Overflow
React Router v4 is different than v1-v3, and optional path parameters aren't explicitly defined in the documentation.
Read more >
Express.js router.param() function - GeeksforGeeks
The parameters of router.param() are name and a function. Where name is the actual name of parameter and function is the callback function....
Read more >
Using parameters in routes - Drupal Wiki
Parameters can be used to pass dynamic values from the URI to the controller.
Read more >
Angular Route Params - TekTutorialsHub
In this tutorial, we learn how to pass route params (Route Parameters) to the Route in Angular. First, let us look at how...
Read more >
Parameters - Slim Framework
To create a URL parameter, prepend “:” to the parameter name in the route URI pattern. When the route matches the current HTTP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found