节点http.request什么都不做 [英] Node http.request does nothing
本文介绍了节点http.request什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var http = require('http');
var options = {
method: 'GET',
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.request(
options,
function(err, resBody){
console.log("hey");
console.log(resBody);
if (err) {
console.log("YOYO");
return;
}
}
);
由于某些原因,这只会超时并且不会将任何内容记录到控制台。
For some reason this just times out and doesn't log anything to the console.
我知道我可以 require('request')
但是我需要使用 http
与我正在使用的插件兼容。
I'm aware that I could require('request')
but I need to use http
to be compatible with a plugin I'm using.
此外,我的版本背景:节点 v0.8.2
Also, background on my versions: Node is v0.8.2
推荐答案
使用此处的示例: http://nodejs.org/api/http.html#http_http_request_options_callback
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
回调没有错误参数,你应该使用on(error,... )
,你的请求不会被发送,直到你调用end()
the callback does not have an error parameter, you should use on("error", ...) and your request doesn't get sent till you call end()
这篇关于节点http.request什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文