Getting req.params in order in Express JS

Posted by Adam Terlson on Stack Overflow See other posts from Stack Overflow or by Adam Terlson
Published on 2013-06-24T20:49:30Z Indexed on 2013/06/25 4:21 UTC
Read the original article Hit count: 160

Filed under:
|
|

In Express, is there a way to get the arguments passed from the matching route in the order they are defined in the route?

I want to be able to apply all the params from the route to another function. The catch is that those parameters are not known up front, so I cannot refer to each parameter by name explicitly.

app.get(':first/:second/:third', function (req) {
    output.apply(this, req.mysteryOrderedArrayOfParams); // Does this exist?
});

function output() {
    for(var i = 0; i < arguments.length; i++) {
        console.log(arguments[i]);
    }
}

Call on GET: "/foo/bar/baz"

Desired Output (in this order):

foo
bar
baz

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about node.js