i'm writing a Web Service in node and i'm new with callbacks concept. I have in my routing file a route that receives url param - a Facebook access token, and should call the getMP function. This function creates http request to Facebook and retrieve some info I want to process later. so the route looks like this:
//route that recives parameter using defined parameters - enter FB access token to get music info about it
app.get('/MP/:FBat',
function (req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
},
function (req, res) {
res.status(200).json(FB.getMP(req.params.FBat));
});
and the function getMP :
exports.getMP = function(access_token) {
request("https://graph.facebook.com/v2.5/me/music?access_token=" + access_token, function(error, response, body) {
if (!error && response.statusCode == 200) {
FB_MP = JSON.parse(body);
// console.log(FB_MP); this returns ok
}else{
console.log(response.statusCode);
}
});
//return value - I wand to use it after the request has returned
return FB_MP;
}
What is the correct way of doing this? It works if the res is fast but of course its not good. any advice ? thanks