gpt4 book ai didi

node.js - nodejs 挂起 express 静态文件

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:51 25 4
gpt4 key购买 nike

问题:如果我注释掉 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);

最佳答案

那个 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);

关于node.js - nodejs 挂起 express 静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com