2012-05-29 116 views
6

問題: 如果我註釋掉express ['static']行,代碼運行完美。如果我包含它(或更改順序),應用程序會在響應之前掛起一段時間。nodejs與快速靜態文件掛起

重新創建: 運行的應用程序,加載了一個瀏覽器,訪問127.0.0.1:51783 不斷刷新頁面(或使用捲曲),控制檯會給你類似的輸出:

GET/1 ms 

然後,當在超時踢和15個請求被髮送,服務器變得沒有反應,你會得到如下:

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); 
+0

我有完全相同的問題。我認爲它來自不同的中間件,但現在已經隔離它connect.static。你有什麼幸運解決這個問題最終Chillbo? –

+0

我們也有這個問題。 –

回答

-2

也有一些是不完全正確有關HTTP請求。我建議使用request模塊來處理這類事情。無論哪種方式工作:

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); 
+0

此答案不響應問題。 –

+1

它有點兒。我來到這裏是因爲我遇到了express.static,http-proxy和http.request的問題。從http.request切換到請求庫解決了問題。 – sheldonh