Node.js https请求错误

我已经从文档中尝试过这个示例,并且效果很好。

但是,当我将URL更改为https://api.mercadolibre.com/sites/ ,请求会挂起。 我唯一得到的是:

 { [Error: socket hang up] code: 'ECONNRESET' } 

这是我的代码:

 var https = require('https'); this.dispatch = function(req, res) { var renderHtml = function(content) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(content, 'utf-8'); } var parts = req.url.split('/'); var options = { host: 'api.mercadolibre.com', port: 443, path: '/sites/', method: 'GET' }; var request = https.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); request.on('error', function(e) { console.error('error'); console.error(e); }); request.end(); return 'item id:' + parts[2]; }; 

我已经试过用curl,soapui和浏览器。 在所有情况下工作很好,但与node.js不。

我怎样才能得到更多的数据呢?

添加

curl我做:curl –sslv3 https://api.mercadolibre.com/sites/作品。 我已经在centos 6中testing过了,也可以工作。 我已经重新安装了节点,这次来自同一个问题。 我的Os是Ubuntu的12.04。 谢谢。

我不知道api.mercadolibre.com网站,但我可以调用API,如果我删除port参数,如下面的代码:

 var options = { host: 'api.mercadolibre.com', path: '/sites/', method: 'GET' }; 

而且我们还需要添加参数来支持SSL版本3:

 https.globalAgent.options.secureProtocol = 'SSLv3_method'; 

由于它正在使用curl,请尝试使用节点curl模块。 我花了一整天的时间尝试使用http和/或https模块在node.js中工作,直到我切换到节点curl为止。

尝试这个:

 var curl = require('node-curl'); curl('https://api.mercadolibre.com/sites/', {SSLVERSION: 3}, function(err, res) { var body = res.body; res.close(); console.log(body); }); 

为什么不使用请求库来处理你的细节?

 var request = require('request'); request('https://api.mercadolibre.com/sites/', {}, function(err, res, body) { console.log("Got body: ", body); }); 

这产生:

 Got body: [{"id":"MLA","name":"Argentina"},{"id":"MLB","name":"Brasil"},{"id":"MCO","name":"Colombia"},{"id":"MCR","name":"Costa Rica"},{"id":"MEC","name":"Ecuador"},{"id":"MLC","name":"Chile"},{"id":"MLM","name":"Mexico"},{"id":"MLU","name":"Uruguay"},{"id":"MLV","name":"Venezuela"},{"id":"MPA","name":"Panamá"},{"id":"MPE","name":"Perú"},{"id":"MPT","name":"Portugal"},{"id":"MRD","name":"Dominicana"}] 

同样在这里,使用curl但不使用node.js.

问题:在CentOS-5 curl上使用openssl库,因此使用centos标准/etc/pki/tls/certs/ca-bundle.crt进行CA检查。

node.js在哪里查找?,通过strace那里我看不到任何参考CA文件进行检查。 Node.js请求服务器与有名的SSL证书从知名的老发行人被接受,但不是对我自己的networking服务器与自己的CA. 我把我自己的CA.crt放在ca-bundle.crt文件中,所以现在curl接受它,而不是node.js.

目前唯一的解决scheme是取消激活我的开箱validation检查:

  var client = require('https'); var download_options = url.parse(sourceUrl); download_options.method = "GET"; download_options.agent = false; download_options.rejectUnauthorized = false; / HERE to accept all SSL-certificates */ var download_request = client.request(download_options); 

我想你是在你需要指定要求的代理之后。 代理设置由libcurl自动检测,该节点使用curl。 所以请求通过节点curl传递。

因此,找出您的组织使用的代理IP和端口,并试试这个:

 var request = require('request'); request({ uri : 'https://mail.google.com/mail', proxy : 'http://<proxy ip>:<proxy port>' }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. }else{ console.log(error); console.log(response.statusCode); } }) 

你会得到ECONNRESET错误,如果你这样做:

 post_options.path = 'history'; ... var req = http.request(post_options, function(res) { ... 

也就是说,你需要确保你的path有这样一个/这样的:

 post_options.path = '/history'; ...