dtn51137 2018-02-05 14:00
浏览 328
已采纳

如何使Node.js有效地处理多个请求?

I am trying to write a Node.js application which accepts incoming requests from client and then make a call to some web service on the remote server to retrieve data.

const express = require('express')
const request = require('request')
const moment = require('moment')

const app = express()

app.get('/', (req, res) => {

    request('http://localhost/sleep.php', (error, response, body) => {
        res.send('get data at ' + moment().format())
    })

})

app.listen(3000)

The remote service is written in PHP:

<?php

    sleep(10);
    echo 'Return something!';

The problem is that if the new request comes in then Node.js is blocked until it has finished the last callback. How to fix this? Any ideas?

Update:

I actually make two requests at the same time via Firefox and the second request spent almost 20 seconds.

Please see the image here

  • 写回答

2条回答 默认 最新

  • drnmslpz42661 2018-02-05 16:45
    关注

    Here's a quick demonstration that concurrent requests for the same URL will not be pipelined by the browser, but different URLs generally will. Adding a distinct value to the query string is a technique to work around this: localhost:3000/?1517849200341 using Date.now() for instance.

    (Broadly speaking, pipelining is disabled in HTTP/1.1, but browsers will use multiple TCP connections to the same end. Pipelining is part of HTTP/2 by setting the maximum number of streams. I don't really know what this means or how to interpret the result below.)

    async function log(fn) {
      console.log(Date());
      await Promise.all(fn());
      console.log(Date());
    }
    
    const req1 = 'https://httpbin.org/delay/1';
    const req2 = 'https://nghttp2.org/httpbin/delay/1';
    
    const req3 = 'https://httpbin.org/delay/1?a';
    const req4 = 'https://httpbin.org/delay/1?b';
    const req5 = 'https://httpbin.org/delay/1?c';
    const req6 = 'https://httpbin.org/delay/1?d';
    
    const req7 = 'https://nghttp2.org/httpbin/delay/1?a';
    const req8 = 'https://nghttp2.org/httpbin/delay/1?b';
    const req9 = 'https://nghttp2.org/httpbin/delay/1?c';
    const req10 = 'https://nghttp2.org/httpbin/delay/1?d';
    
    btn1.addEventListener('click', () => log(() => [
      fetch(req1),
      fetch(req1),
      fetch(req1),
      fetch(req1),
      fetch(req1),
    ]));
    
    btn2.addEventListener('click', () => log(() => [
      fetch(req2),
      fetch(req2),
      fetch(req2),
      fetch(req2),
      fetch(req2),
    ]));
    
    btn3.addEventListener('click', () => log(() => [
      fetch(req1),
      fetch(req3),
      fetch(req4),
      fetch(req5),
      fetch(req6),
    ]));
    
    btn4.addEventListener('click', () => log(() => [
      fetch(req2),
      fetch(req7),
      fetch(req8),
      fetch(req9),
      fetch(req10),
    ]));
    <button id=btn1>HTTP/1.1, same URLs</button>
    <button id=btn2>HTTP/2, same URLs</button>
    <button id=btn3>HTTP/1.1, different URLs</button>
    <button id=btn4>HTTP/2, different URLs</button>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog