I'm new to nodejs and express and trying to build a simple functionality
I have this piece of code which successfully sends my get requests to the appropriate route (in this case a service where I have a mysql query written)
app.get('/requests', service.getRequests);
This works with simple get request like http://localhost:5000/requests
I'm wondering how I can get a parameter from the get request and pass it in the route handler ?
For example: something like http://localhost:5000/requests?id=123 I'd like to pass in the id to the service.getRequests function, but the below doesn't work -
app.get('/request',function(req,res){
res.send(service.getRequests(req));
});
Here's how service.js (part of it) looks like
exports.getRequests = function(req,res){
//console.log(req.id);
connection.connect()
connection.query('SELECT * FROM table1 t where t.uid = ? LIMIT 10',[req.id], function (err, result, fields) {
if (err) throw err
res.json({ results: result });
})
connection.end()
}
EDIT : My question specifically is not how to get a GET parameter but how do I use it with res.send / call a service with this parameter, specifically the below line -
app.get('/request',function(req,res){
res.send(service.getRequests(req));
});