KickinMhl KickinMhl - 1 year ago 137
Node.js Question

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:

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):

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?


Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download