Node.js Express块 [英] Node.js Express block

查看:74
本文介绍了Node.js Express块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我计划使用Express缓存在一定时间范围内接收到的所有请求,直到我一次发送所有响应为止. 但不幸的是,直到我回复了第一个请求,我才能收到第二个请求.因此,我猜想node/express会以某种方式阻止其他请求的进一步处理.

My Problem is, that I'm planning to use express to cache all requests which I receiver for a certain amount of time until I send all responses at once. But unfortunately I can't receive a second request until I've responded to the first one. So I guess node / express is somehow blocking the further processing of other requests.

我为您构建了一个最小的工作示例,因此您可以更好地了解我在说什么.

I build a minimal working example for you, so you can see better what I'm talking about.

var express = require('express');
var app = express();

var ca = [];

app.get('/hello.txt', function(req, res){
  ca.push(res);
  console.log("Push");
});

setInterval(function(){
  while (ca.length) {
    var res = ca.shift();
    res.send('Hello World');
    console.log("Send");
  }
},9000);

var server = app.listen(3000, function() {
  console.log('Listening on port %d', server.address().port);
});

当我仅向localhost:3000发送一个请求并等待9秒时,我就可以发送第二个请求.但是,当我发送两个通知而不等待间隔的回调时,第二个将被阻塞,直到第一个间隔被触发为止.

When I'm sending just one request to localhost:3000 and wait for 9sec I'm able to send a second one. But when I send both without waiting for the callback of the interval, the second one is blocked until the first interval triggered.

长话短说:为什么会发生这种阻塞?有什么方法可以避免这种阻塞.

Long Story short: Why is this blocking happening and what ways are there to avoid this blocking.

PS:默认的http包似乎显示了另一种行为 http://blog.nemikor.com/2010/05/21/long-polling-in-nodejs/

PS: It seems that the default http package shows another behavior http://blog.nemikor.com/2010/05/21/long-polling-in-nodejs/

推荐答案

尝试使用Firefox和chrome来防止序列化请求...

try it with firefox and chrome to prevent serializing the requests...

这篇关于Node.js Express块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆