我不确定你看到了什么。当我运行测试时,我得到了预期的结果——请求\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 秒后完成,这意味着它们实际上是并行处理的(正如预期的那样)。