【问题标题】:express.js does not async call the same router callback?express.js 不异步调用相同的路由器回调?
【发布时间】:2012-10-14 13:52:32
【问题描述】:

当调用'/a'时,'/b'可以立即执行。但是如果我调用另一个'/a',第二个会等待第一个结束。如何让 '/a' 真正异步调用?

代码:

app.get '/a', (req, res, next) ->
  f = ->
    res.send 'a' 
    console.log 'end', new Date()
  console.log 'sleep', new Date()
  setTimeout f, 10000

app.get '/b', (req, res, next) ->
  res.send 'b'

输出:

Express server listening on port 3000 in development mode
sleep Sun Oct 14 2012 12:37:52 GMT+0800 (SGT)
GET /b 200 9ms - 1
end Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
GET /a 200 10022ms - 1
sleep Sun Oct 14 2012 12:38:02 GMT+0800 (SGT)
end Sun Oct 14 2012 12:38:12 GMT+0800 (SGT)
GET /a 200 10005ms - 1

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    我明白了,因为我在同一个浏览器上运行了两个 '/a'。我只是尝试在 chromium 中运行一个,在 Firefox 中运行另一个,它们是异步处理的。看起来很有趣。

    【讨论】:

    • 对,因为你直到睡眠后才执行res.send,你会挂起浏览器,因为它正在等待响应。
    【解决方案2】:

    我不确定你看到了什么。当我运行测试时,我得到了预期的结果——对\a 的请求是异步处理的。如果您尝试以下代码并使用DEBUG=* node app.js 执行它,您会得到与我相同的结果吗?

    var express = require('express'),
        app = express();
    
    app.get('/a', function (req, res, next) {
      var f = function () {
        res.send('a');
        console.log('end', new Date());
      };
    
      console.log('sleep', new Date());
      setTimeout(f, 10000);
    });
    
    app.get('/b', function (req, res, next) {
      res.send('b');
    });
    
    app.listen(4000);
    

    这是运行两个对\a 的请求和一个对\b 的请求的输出,而前两个请求正在休眠。

      express:router dispatching GET /a (/a) +52s    // first call to \a
      express:router matched get /a +1ms
    sleep Sat Oct 13 2012 21:59:42 GMT-0700 (PDT)     
      connect:dispatcher query +530ms
      connect:dispatcher expressInit +1ms
      connect:dispatcher router +0ms
      express:router dispatching GET /a (/a) +530ms  // second call to \a in parallel
      express:router matched get /a +0ms
    sleep Sat Oct 13 2012 21:59:42 GMT-0700 (PDT)
      connect:dispatcher query +874ms
      connect:dispatcher expressInit +0ms
      connect:dispatcher router +0ms
      express:router dispatching GET /b (/b) +874ms  // call to \b handled immediately
      express:router matched get /b +0ms
    end Sat Oct 13 2012 21:59:52 GMT-0700 (PDT)   // first call to \a ends
    end Sat Oct 13 2012 21:59:52 GMT-0700 (PDT)   // second call ends at same time
    

    您可以看到对 \b 的请求立即完成,然后对 \a 的两个请求都在 10 秒后完成,这意味着它们实际上是并行处理的(正如预期的那样)。

    【讨论】:

      【解决方案3】:

      Bill,我的代码得到了不同的结果。我通过浏览器运行这些。我在只有一个内核的 Linux 上使用 Express 3。

        express:application booting in development mode +0ms
        connect:dispatcher use / query +0ms
        connect:dispatcher use / expressInit +0ms
        connect:dispatcher use / router +2ms
        express:router defined get /a +0ms
        express:router defined get /b +1ms
        connect:dispatcher query +12s
        connect:dispatcher expressInit +2ms
        connect:dispatcher router +0ms
        express:router dispatching GET /a (/a) +12s
        express:router matched get /a +0ms
      sleep Sun Oct 14 2012 13:20:37 GMT+0800 (SGT)
        connect:dispatcher query +3s
        connect:dispatcher expressInit +0ms
        connect:dispatcher router +0ms
        express:router dispatching GET /b (/b) +3s
        express:router matched get /b +1ms
      end Sun Oct 14 2012 13:20:47 GMT+0800 (SGT)
        connect:dispatcher query +6s
        connect:dispatcher expressInit +0ms
        connect:dispatcher router +0ms
        express:router dispatching GET /a (/a) +6s
        express:router matched get /a +0ms
      sleep Sun Oct 14 2012 13:20:47 GMT+0800 (SGT)
      end Sun Oct 14 2012 13:20:57 GMT+0800 (SGT)
      

      【讨论】:

        猜你喜欢
        • 2019-09-17
        • 2015-05-03
        • 1970-01-01
        • 2019-03-11
        • 2016-03-03
        • 2015-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多