我正在尝试使用 sails.js 在 Controller 中访问来自 facebook 的信息。这是我的代码:
module.exports = {
commonLikes : function(req,res){
var other_uid = req.param('uid');
//var me = req.params.user.uid;
console.log(other_uid);
User.findOne({uid : other_uid}).done( function(err,data){
var other = data;
var http = require('http'), options = {
host : "https://graph.facebook.com",
port : 80,
path : "/"+other.uid+"/likes?access_token="+other.token,
method : 'GET'
};
var webservice_data = "";
var webservice_request = http.request(options, function(webservice_response)
{
webservice_response.on('error', function(e){ console.log(e.message); });
webservice_response.on('data', function(chunk){ webservice_data += chunk;});
webservice_response.on('end', function(){res.send(webservice_data);});
console.log('coucou');
});
});
}
};
虽然 http.request 函数看起来不起作用,但我在尝试访问/Likes/commonLikes?uid=XXXXX 时遇到错误。
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
有人知道如何使用 sails 访问外部 API 吗?
谢谢,
最佳答案
host
不应包含协议(protocol):
var http = require('http'), options = {
host : "graph.facebook.com",
port : 80,
path : "/"+other.uid+"/likes?access_token="+other.token,
method : 'GET'
};
如果你想使用https
,请使用https
模块而不是http
,并使用端口443。不要忘记完成请求后添加 req.end()
。
关于facebook - 如何使用 sails.js 访问外部 api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18451158/