简体   繁体   中英

How to send more than 1 parameters in url and how to access them in my GET route in Node.js/Express

Currently I'm successfully able to send 1 parameter and then access it in my GET router, however, I am not sure how to add more than 1 parameters and how to access them in my GET router. Here's my code showing how I send and get 1 parameter.

EJS file:

<a class='stickyContainer homePage' href='/image/<%= results[i].id %>'></a>

Router file:

router.get('/:id', function(req, res, next) {
    res.send(req.params.id);
});

Now when I click any of my images, I am send to a URL like that http://localhost:8087/image/21 , however I would like to be send to something like http://localhost:8087/image/Beautiful-Image-21 , where Beautiful-Image is the name of the image and 21 is the Id of the image. How am I supposed to chain the parameters like that?

Make your route like this

router.get('/:name/:id', function(req, res, next) {
    console.log(req.params.id)
    console.log(req.params.name)
    res.send(req.params.id);
});

And call it like this

http://localhost:8087/image/Beautiful-Image/21

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