Search code examples
node.jsexpressodata

Express Js - Change the way parameter is passed to an ODATA style


I was wondering if anyone has tried / or know, of a way to make express accept ODATA style parameters. For instance:

Standard Express style incoming parameter example:

http://www.blah.com/user/1234

app.get('/:user_id', function (req, res){ 
    console.log('Test user_id param: ' + req.params.user_id);
});

ODATA style incoming parameter example (this is what I want to do):

http://www.blah.com/user(1234)

app.use('\(:user_id\)', function (req, res){ 
        console.log('Test param: ' + req.params.user_id);
    });

Has anyone done something like this? Suggestions on how to implement something close to this?

Thanks.


Solution

  • As @jfriend00 suggests, you can provide your own regex to Express' route resolver. Something like this should work for your use case:

    app.get(/^\/user\((.*)\)$/, function (req, res) {
        // Your user id is now in req.params[0]
    }
    

    See docs.

    According to docs, it should also work with "simpler" style regex inspired routing with just a wild card, like so:

    app.get("/user(*)", function (req, res) {
        // still in req.params[0]
    }