Node.js来自http请求的响应不会调用'end'事件而不包括'data'事件 [英] Node.js response from http request not calling 'end' event without including 'data' event

查看:530
本文介绍了Node.js来自http请求的响应不会调用'end'事件而不包括'data'事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的客户端应用程序与node.js中的服务器端应用程序通信。在客户端,我有以下代码:

So I have a simple client application communicating with a server side application in node.js. On the client side, I have the following code:

function send (name) {
    http.request({
        host: '127.0.0.1',
        port: 3000,
        url: '/',
        method: 'POST'
    }, function (response) {
        response.setEncoding('utf8');
        response.on('data', function (data) {
           console.log('did get data: ' + data);
        });
        response.on('end', function () {
            console.log('\n   \033[90m request complete!\033[39m');
            process.stdout.write('\n your name: ');
        });
        response.on('error', function (error) {
            console.log('\n Error received: ' + error);
        });
    }).end(query.stringify({ name: name})); //This posts the data to the request
}

奇怪的是,如果我不要通过以下方式包含数据事件:

The odd part is, if I don't include the 'data' event via:

    response.on('data', function (data) {
       console.log('did get data: ' + data);
    });

响应的结束事件永远不会被关闭。

The 'end' event for the response is never fired off.

服务器代码如下:

var query = require('querystring');
require('http').createServer(function (request, response) {
    var body = '';
    request.on('data', function (data) {
       body += data;
    });
    request.on('end', function () {
        response.writeHead(200);
        response.end('Done');
        console.log('\n got name \033[90m' + query.parse(body).name + '\033[39m\n');
    });
}).listen(3000);

我想知道为什么当文档(据我所知)不需要时你要收听数据事件以关闭响应会话。

I would like to know why this is happening when the documentation (to my knowledge) doesn't require you to listen in on the data event in order to close a response session.

推荐答案

'仅在所有数据被使用时被调用,请检查以下参考:

The 'end' is invoked only when all the data was consumed, check the reference below:


事件:'end'

Event: 'end'

当不再提供更多数据时,此事件触发。

This event fires when no more data will be provided.

请注意,结束事件不会触发,除非数据完全是
消耗。这可以通过切换到流模式,或者通过
重复调用read()直到结束为止。

Note that the end event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by calling read() repeatedly until you get to the end.

但是为什么你需要调用 .on('data',..)?答案是

But why you need to call the .on('data',..)? The answer is


如果你附加一个数据事件监听器,那么它将把流
切换成流模式,数据将一旦
可用,就被传递给你的处理程序。

If you attach a data event listener, then it will switch the stream into flowing mode, and data will be passed to your handler as soon as it is available.

所以基本上通过添加数据监听器,它会将流更改为流模式并开始使用数据。

So basically by adding the data listener, it changes the stream into flowing mode and starts consuming the data.

请检查此链接以获得更多的参考。

Please check this link for more reference about it.

这篇关于Node.js来自http请求的响应不会调用'end'事件而不包括'data'事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆