The goal is to deliver files with a nodejs http server.
The problem is the transmission from the server response to the browser.
I deliver a string as content as shown in program code.
VPS: debian 7 / SSD / 2 core / ~30ms ping
Related: node.js response.write(data) taking long time when size of data is small
Some string size benchmarking (content-download time)
- 1 kb: 0,18 ms - 10 kb: 7,00 ms - 20 kb: 47,50 ms - 30 kb: 55,00 ms - 40 kb: 58,10 ms - 50 kb: 86,20 ms - 60 kb: 93,10 ms - 80 kb: 107,10 ms - 100 kb: 120,00 ms (nginx needs 0.31ms)
Program code:
// var request => the http incoming request
request.socket.setNoDelay();
request.connection.setNoDelay();
// var response => the response from the http incoming request
response.on('close', function () {
console.log("[ABORTED][%s] %s", request.connection.remoteAddress, request.url);
});
response.on('finish', function () {
console.log("[SUCCESS][%s] %s", request.connection.remoteAddress, request.url);
});
// get the content of the file (takes ~0ms)
var fileContent = fileSystem.readFileSync(filePath, 'utf8');
// Configure response header
response.writeHead(200, {
'content-length': ''+fileContent.length,
'content-type': 'text/plain',
});
// Send content
response.end(fileContent, 'utf8', function(){
console.log("Ending response took %sms", Date.now()-timeB2);
});