简体   繁体   中英

Hyphen in express route parameters

does anyone know if I can format my express route & parameters like:

app.get('/:a-:b-:c', (req, res) => {
 // a, b, c are parameters
});

Thanks in advance!

This is the proper way to handle multiple parameters, you will nest each parameter as an additional part of the route Ive included a link to the routing page of express which also goes over this https://expressjs.com/en/guide/routing.html

 app.get('/:a/:b/:c', function(req, res) {
        var data = {
            "data": {
                "a": req.params.a,
                "b": req.params.b,
                "c": req.params.c

            }
        }; 

        send.json(data);
    });

You just can go with regex routes, tried to search something with named capturing groups in js but is not possible so a trick from es6 will come very handy.

router.get(/^\/(\w+)-(\w+)-(\w+)?$/, function(req, res){
   const [a, b, c] = req.params;
   // a = req.params[0];
   // b = req.params[1];
   // c = req.params[2];

   // implement your logic
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM