Nodejs freezes with pronounced static files

Problem: If I comment out the line express ['static'], the code will work fine. If I turned it on (or reordered), the application freezes for a while before responding.

Re-creation: Launch the application, load the browser and go to 127.0.0.1ל1783 Refresh the page constantly (or use curl), the console will give you a result similar to:

GET / 1 ms 

Then, when a timeout is sent and 15 requests are sent, the server becomes unresponsive, and you get the following:

 Server Now Unresponsive but requests queued GET / 35549 ms 

app.js

 var http = require('http'); var express = require('express'); var app = express.createServer(); app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' })); app.use(express.bodyParser()); app.use(express['static'](__dirname + '/')); //Comment me and all works perfectly! app.listen(51783); http.globalAgent.maxSockets = 500; //doesn't help setTimeout(function(){ console.log('Server Now Unresponsive but requests queued'); [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15].forEach(function(item){ http.request({host:'http://cnn.com', port:80, path:'/null', method:'GET'}, function(res){ }).on('error', function(e){}); }); },5000); 
+6
source share
1 answer

There is something wrong with this HTTP request. I recommend using request for this kind of thing. In any case, this works:

 var http = require('http'); var express = require('express'); var app = express.createServer(); app.configure(function(){ app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' })); app.use(express.bodyParser()); app.use(express['static'](__dirname + '/public')); //Comment me and all works perfectly! }) app.listen(51783); var request = require('request'); setTimeout(function(){ console.log('Server Now Unresponsive but requests queued'); [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15].forEach(function(item){ request('http://www.google.com', function (error, response, body) { console.log("request number "+item+" received") }) }); },5000); 
-2
source

Source: https://habr.com/ru/post/916936/


All Articles