Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express route with param overriding the static route

I have a code like below

app.get('/all', callback2);
app.get('/:id', callback);

/:id is overriding the route /all.

like image 653
pradyumnad Avatar asked Nov 20 '25 08:11

pradyumnad


1 Answers

The only solution I can think of, other than avoiding the use of potentially ambiguous routes, is to implement one route like this:

app.get('/:id', function(req, res) {
    if (req.params.id === 'all') {
        // do what you would do for /all
    } else {
        // do what you do for /:id
    }
}
like image 121
Jim B. Avatar answered Nov 23 '25 01:11

Jim B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!