I've found some questions that seems has some relationship with this question(eg:Why is node.js only processing six requests at a time?), but I still can not understand the details.
the following is my case.
first, server's code:
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!');
and then, the client's code:
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();
}
run both of them in node, of course run server first, the client will send 10 request in about 5s, and everything is alright. like this:
But, if I remove the code about listening the "data" event in client, like this:
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);
});
});
run again, the client seems has send 10 request in 5s, but in fact, the server only received 5 request:
and as you see, the "end" event seems doesn't triggered.
at last, I add a header in the response via the server's code:
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();
});
restart the server and run client again:
now the server can received all the 10 request immediately, but the "end" event seems still doesn't triggered.
So, it seems that the listener on "data" event has taken some effect on the http connection.
can anyone explain the details for everyone?