我是node.js的新手。当尝试通过节点使用GET方法发出http请求时,程序将打印“ Got Response:302”并保持在那里而没有退出。根据代码,它必须在打印后从节点出来。无法理解节点不退出程序就等待某事的原因。
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
默认情况下,在节点v0.10 +中,可读流以暂停状态开始,以防止数据丢失。因此,如果有等待响应的数据,则需要消耗响应以使进程自然退出:
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
// this forces streams1 behavior and starts emitting 'data' events
// which we ignore, effectively draining the stream ...
res.resume();
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句