nodejs挂起了快速静态文件

时间:2012-05-29 18:21:35

标签: node.js static express

问题: 如果我注释掉快速['static']行,代码运行完美。如果我包含它(或更改订单),应用程序会在响应之前挂起一段时间。

重新创建: 运行应用程序,加载浏览器并转到127.0.0.1:51783 不断刷新页面(或使用curl),控制台将为您提供类似于:

的输出
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);

1 个答案:

答案 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);