从函数内发出 http 请求 - Node.js
Make http request from within a function - Node.js
我正在节点中编写 Web 服务,我是回调概念的新手。
我的路由文件中有一个接收 url 参数的路由 - 一个 Facebook 访问令牌,并且应该调用 getMP 函数。此函数创建对 Facebook 的 http 请求并检索一些我想稍后处理的信息。
所以路线看起来像这样:
//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));
});
和函数 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;
}
正确的做法是什么?如果 res 很快,它就可以工作,但当然不好。
有什么建议吗?
谢谢
你可以使用像 q 这样的承诺库,并在方法 then
中获得必要的值后做出响应,试试这样的事情:
//route that receives 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) {
FB.getMP(req.params.FBat)
.then(function(result){
if(result){
res.status(200).json(result);
}
else{
console.log('do something about it');
}
});
});
getMP
的代码将是这样的:
var Q = requirie('q');
exports.getMP = function(access_token) {
var deferred = Q.defer();
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);
deferred.resolve(FB_MP);
}else{
deferred.resolve(false);
}
});
//return value - I wand to use it after the request has returned
return deferred.promise;
}
您还可以将 req
和 res
参数传递给您放置逻辑的一种控制器,然后将结果发送到视图。像这样:
//route that receives parameter using defined parameters - enter FB access token to get music info about it
var controller = require('path/to/controller');
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) {
controller.getFacebookAccess(req, res, req.params.FBat);
});
控制器:
module.exports = function getFacebookAccess(req, res, 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);
res.status(200).json(FB_MP);
}else{
res.send('do something about it');
}
});
}
};
我正在节点中编写 Web 服务,我是回调概念的新手。 我的路由文件中有一个接收 url 参数的路由 - 一个 Facebook 访问令牌,并且应该调用 getMP 函数。此函数创建对 Facebook 的 http 请求并检索一些我想稍后处理的信息。 所以路线看起来像这样:
//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));
});
和函数 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;
}
正确的做法是什么?如果 res 很快,它就可以工作,但当然不好。 有什么建议吗? 谢谢
你可以使用像 q 这样的承诺库,并在方法 then
中获得必要的值后做出响应,试试这样的事情:
//route that receives 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) {
FB.getMP(req.params.FBat)
.then(function(result){
if(result){
res.status(200).json(result);
}
else{
console.log('do something about it');
}
});
});
getMP
的代码将是这样的:
var Q = requirie('q');
exports.getMP = function(access_token) {
var deferred = Q.defer();
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);
deferred.resolve(FB_MP);
}else{
deferred.resolve(false);
}
});
//return value - I wand to use it after the request has returned
return deferred.promise;
}
您还可以将 req
和 res
参数传递给您放置逻辑的一种控制器,然后将结果发送到视图。像这样:
//route that receives parameter using defined parameters - enter FB access token to get music info about it
var controller = require('path/to/controller');
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) {
controller.getFacebookAccess(req, res, req.params.FBat);
});
控制器:
module.exports = function getFacebookAccess(req, res, 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);
res.status(200).json(FB_MP);
}else{
res.send('do something about it');
}
});
}
};