bgc*_*ode 5 javascript http node.js
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;
}
}
);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这只会超时,并且不会将任何内容记录到控制台.
我知道我可以,require('request')但我需要使用http与我正在使用的插件兼容.
此外,我的版本的背景:节点是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();
Run Code Online (Sandbox Code Playgroud)
回调没有错误参数,您应该使用 on("error", ...) 并且您的请求在您调用 end() 之前不会发送
| 归档时间: |
|
| 查看次数: |
1600 次 |
| 最近记录: |