I use express on NodeJS, and I want to get a result of a callback function.
function yts(s) {
return youTube.search(s, 5, function(error, result) {
var res;
var json = []
if (error) {
console.log(error);
res.send("error");
}
else {
//console.log(JSON.stringify(result, null, 2));
res = result["items"];
var i = 0;
while (res[i]) {
//console.log(i + " | " + res[i]["id"]["videoId"] + " | " + res[i]["snippet"]["title"] + " | " + "https://img.youtube.com/vi/"+ res[i]["id"]["videoId"] +"/0.jpg");
json.push({videoID: res[i]["id"]["videoId"], title: res[i]["snippet"]["title"]});
i++;
}
console.log(json);
//Get json
}
});
}
app.get('/search/:title', function (req, res) {
res.send(JSON.stringify(yts(req.params["title"])));
});
I am using youtube-node (NPM) to search youtube and returning the most important inforamtion back to the User. How can I make that //Get json return the code somehow back to app.get function.