adc*_*c06 4 facebook node.js sails.js
我正在尝试使用sails.js从控制器中的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');
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
虽然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)
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用sails访问外部API?
谢谢,
该host不该包括的协议:
var http = require('http'), options = {
host : "graph.facebook.com",
port : 80,
path : "/"+other.uid+"/likes?access_token="+other.token,
method : 'GET'
};
Run Code Online (Sandbox Code Playgroud)
如果要使用https,请使用https模块代替http,并使用端口443.并且req.end()在完成请求后不要忘记添加.
| 归档时间: |
|
| 查看次数: |
4608 次 |
| 最近记录: |