I am trying to send more than one parameter to the GET function. Right now I have:
const response = await fetch(`http://localhost:5000/demos/price/${numticket}/${carry}`);
And I am trying to send this information to (but the request does not go to the function):
app.get('/demos/price/:ticket/:carry', async(req, res)=>{
//console.log('in index');
try{
let ticket = req.params.ticket;
let carry = req.params.carry;
} catch(err){
console.log(err.message);
}
});
Is this how I go about doing this or do I have it completely wrong?
CodePudding user response:
You're capturing path parameters correctly, except you don't need the try/catch block around setting variables since you're not doing anything that can throw an error. This works as expected for me:
app.get('/demos/price/:ticket/:carry', (req, res)=>{;
res.send(req.params.ticket ' ' req.params.carry)
});
(Sent request in my terminal via curl like so: curl localhost:3000/demos/price/aaa/bbb
, but the way you're sending it in JS is correct as well)
Is it possible that it's working properly but you're just not noticing anything happening because you're just setting variables and not logging anything or returning a response?