简体   繁体   English

如何在nodejs / express中异步发送响应

[英]How to send response asynchronously in nodejs / express

     const express = require("express");
     const router = express.Router();

     router.get("/", async (req, res, next) => {
         setTimeout(() => {
             res.send("Hello Stackoverflow");
         }, 10000);
     });

     module.exports = router;

When i send a get request from two browser tabs at same time first request gets completed in 10 seconds and second one takes 20 seconds.当我同时从两个浏览器选项卡发送获取请求时,第一个请求在 10 秒内完成,第二个请求需要 20 秒。

I expected both the requests to get completed in 10 seconds as i used setTimeout to make it async.我希望这两个请求都能在 10 秒内完成,因为我使用 setTimeout 使其异步。 Am i doing something wrong here or nodejs does't take another request before completing the first one?我在这里做错了什么还是nodejs在完成第一个请求之前没有接受另一个请求?

What i want this code to do is:我想要这段代码做的是:

request ──> setTimeout请求──> setTimeout
request ──> setTimeout请求──> setTimeout
setTimeout completes ──> send response setTimeout 完成 ──> 发送响应
setTimeout completes ──> send response setTimeout 完成 ──> 发送响应

what it currently doing is:它目前正在做的是:

request ──> setTimeout请求──> setTimeout
setTimeout completes ──> send response setTimeout 完成 ──> 发送响应
request ──> setTimeout请求──> setTimeout
setTimeout completes ──> send response setTimeout 完成 ──> 发送响应

How can i achieve this?我怎样才能做到这一点?

EDIT =>编辑 =>
I suspect this is some chrome only behaviour as the code works in firefox.我怀疑这是一些仅限 chrome 的行为,因为代码在 firefox 中有效。 Also if i open one normal tab and another incognito tab it works as expected.此外,如果我打开一个普通选项卡和另一个隐身选项卡,它会按预期工作。

This is caused by your browser wanting to reuse the connection to the Express server for multiple requests because by default, Express (actually, Node.js's http server does this) is telling it to by setting the header Connection: keep-alive (see below for more information).这是由于您的浏览器想要为多个请求重用与 Express 服务器的连接,因为默认情况下,Express(实际上,Node.js 的http服务器会这样做)通过设置 header Connection: keep-alive (见下文了解更多信息)。

To be able to reuse the connection, it'll have to wait for a request to finish before it can send another request over the same connection.为了能够重用连接,它必须等待请求完成,然后才能通过同一连接发送另一个请求。

In your case, the first request takes 10 seconds, and only then is the second request sent.在您的情况下,第一个请求需要 10 秒,然后才发送第二个请求。

To illustrate this behaviour, try this:为了说明这种行为,试试这个:

     router.get("/", async (req, res, next) => {
         res.set('connection', 'close');
         setTimeout(() => {
             res.send("Hello Stackoverflow");
         }, 10000);
     });

This tells the browser that the connection should be closed after each request, which means that it will create a new connection for the second request instead of waiting for the first request to finish.这告诉浏览器应该在每次请求后关闭连接,这意味着它将为第二个请求创建一个新连接,而不是等待第一个请求完成。

For more information, look here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection有关更多信息,请查看此处: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM