6

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

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

GET / 1 ms

然后,当超时启动并发送 15 个请求时,服务器变得无响应,您会得到以下信息:

Server Now Unresponsive but requests queued
GET / 35549 ms

应用程序.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);
4

1 回答 1

-2

该 http 请求有些地方不太对劲。我建议对这类事情使用请求模块。无论哪种方式,这都有效:

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);
于 2012-06-03T11:51:02.530 回答