【发布时间】:2015-01-12 08:18:18
【问题描述】:
我发现了一些似乎与这个问题有关系的问题(例如:Why is node.js only processing six requests at a time?),但我仍然无法理解细节。
以下是我的情况。
首先,服务器的代码:
var express = require ('express'),
methodOverride = require('method-override');
var app = express();
app.use(methodOverride());
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500);
res.send({ error: err });
});
app.use('/', function(req, res, next) {
res.header('Cache-control', 'no-cache');
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.get('/OK/', function (req, res) {
console.log(getTimestamp() + ' OK ');
res.status(200).send("200");
});
app.listen(22222);
function getTimestamp(){
var timestamp = new Date();
return timestamp.toString() + " " + timestamp.getMilliseconds();
}
console.log(getTimestamp() + ' Sever started!');
然后是客户端的代码:
var http = require('http');
var opt = {
method: "GET",
host: "localhost",
port: 22222,
path: "/OK/",
headers: {
'Cache-control': 'no-cache'
}
};
count = 10;
function request(){
var req = http.request(opt, function (serverFeedback) {
var body = "";
serverFeedback
.on('data',function(){})
.on('end', function () {
console.log(getTimestamp(), "response END", serverFeedback.statusCode, body);
});
});
req.end();
console.log(getTimestamp(), "resuest START");
count--;
if(count > 0){
setTimeout(request, 500);
}
}
request();
function getTimestamp(){
var timestamp = new Date();
return timestamp.toString() + " " + timestamp.getMilliseconds();
}
在node中运行它们,当然先运行server,客户端大约5s会发送10个请求,一切正常。像这样:
但是,如果我删除有关在客户端监听“数据”事件的代码,如下所示:
var req = http.request(opt, function (serverFeedback) {
var body = "";
serverFeedback
//.on('data',function(){}) /* remove the listener*/
.on('end', function () {
console.log(getTimestamp(), "response END", serverFeedback.statusCode, body);
});
});
再次运行,客户端似乎在 5s 内发送了 10 个请求,但实际上,服务器只收到了 5 个请求:
如您所见,“结束”事件似乎没有触发。
最后,我通过服务器的代码在响应中添加了一个标头:
app.use('/', function(req, res, next) {
res.header('Cache-control', 'no-cache');
res.header('Access-Control-Allow-Origin', '*');
res.header('connection', 'close'); /* add a header about connection */
next();
});
重启服务器并再次运行客户端:
现在服务器可以立即接收到所有 10 个请求,但是“结束”事件似乎仍然没有触发。
所以,“data”事件的监听器似乎对http连接产生了一些影响。
谁能给大家解释一下细节?
【问题讨论】:
标签: javascript node.js http