通过node.js中的http.request()在每个请求中获取请求超时

我使用nodejs http.request方法将一些数据发布到外部API。 我testing像这样:

var options={ hostname:'www.google.com', port:80, 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); }); }); //console.log(req); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); 

这整个包裹在一个快递服务器后的路线。 但在控制台,它总是说在一段时间后连接超时。 我无言以对

你只是简单地创build一个request对象。 你也必须发送它。 使用req.end()来实际发送请求。

 req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.end(); //add it