express.router()的路由是否同步?

时间:2017-02-20 07:06:07

标签: node.js express

这令我感到困惑......以下测试显示异步接收请求,异步响应,但所有请求都是先收到,然后响应...给出以下3个文件:

的package.json

{
    "name": "express-router-sync-test",
    "version": "1.0.0",
    "description": "Testing if express router has sync aspect",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "body-parser": "1.16.0",
        "express": "4.14.1",
        "request": "2.79.0"
    }
}

server.js

const express = require('express');
const bodyParser = require('body-parser');
const router = express.Router();
const request = require('request');

// Create our Express application
let app = express();

let port = 1111;

// body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(router);

// Start the server
app.listen(port);
console.log('Server listening on port ' + port + '.');


router.get('/', _get);
router.post('/test', _test);

function _test(req, res) {
    console.log('starting to process request # ', req.body.requestNumber);
    request({
        method: 'GET',
        url: 'http://localhost:1111'
    }, (err, response, body) => {
        console.log('process # ' +  req.body.requestNumber + ' has ended');
    });
}

function _get(req, res) {
    res.json({ success: true });
}

test.js

const request = require('request');

let i;
let len = 500;
let options = {
    method: 'POST',
    url: 'http://localhost:1111/test'
}

for (i = 0; i < len; i++) {
    options.form = { requestNumber: i + 1 };
    request(options, (err, response, body) => {
        if (err) console.log(err);
        if (response) console.log(body)
    }); 
}

给出dir结构:

app/
--package.json
--server.js
--test.js

按照以下步骤重现测试:

  • 在终端中,导航到应用目录并运行npm install
  • 运行node server.js
  • 打开另一个终端,导航到app dir并运行node test.js

如果查看server.js终端的输出,您会注意到所有&#34;启动进程&#34;记录按特殊顺序组合在一起。您还会注意到所有&#34;流程已结束&#34;记录组合在一起,但也以异步顺序组合。

我现在问两个问题:

  • 为什么在收到所有请求之前推迟回复?
  • 这是否发生是因为我自己充斥着请求?

非常感谢任何信息。

编辑:这是此测试运行的示例日志,包含50个请求而不是500个。您将注意到在收到所有请求之前没有响应任何请求。您发送的请求越多,您等待第一个响应的时间就越长。

starting to process request #  1
starting to process request #  2
starting to process request #  3
starting to process request #  4
starting to process request #  5
starting to process request #  6
starting to process request #  9
starting to process request #  8
starting to process request #  7
starting to process request #  10
starting to process request #  12
starting to process request #  11
starting to process request #  13
starting to process request #  17
starting to process request #  16
starting to process request #  15
starting to process request #  14
starting to process request #  21
starting to process request #  19
starting to process request #  20
starting to process request #  18
starting to process request #  22
starting to process request #  23
starting to process request #  25
starting to process request #  24
starting to process request #  27
starting to process request #  28
starting to process request #  26
starting to process request #  32
starting to process request #  31
starting to process request #  30
starting to process request #  29
starting to process request #  36
starting to process request #  35
starting to process request #  33
starting to process request #  34
starting to process request #  40
starting to process request #  38
starting to process request #  39
starting to process request #  37
starting to process request #  44
starting to process request #  42
starting to process request #  43
starting to process request #  41
starting to process request #  45
starting to process request #  46
starting to process request #  47
starting to process request #  49
starting to process request #  48
starting to process request #  50
process # 1 has ended
process # 2 has ended
process # 4 has ended
process # 3 has ended
process # 5 has ended
process # 6 has ended
process # 9 has ended
process # 8 has ended
process # 11 has ended
process # 12 has ended
process # 10 has ended
process # 7 has ended
process # 13 has ended
process # 17 has ended
process # 15 has ended
process # 16 has ended
process # 14 has ended
process # 21 has ended
process # 22 has ended
process # 18 has ended
process # 20 has ended
process # 19 has ended
process # 27 has ended
process # 24 has ended
process # 25 has ended
process # 23 has ended
process # 31 has ended
process # 32 has ended
process # 26 has ended
process # 28 has ended
process # 30 has ended
process # 29 has ended
process # 34 has ended
process # 35 has ended
process # 33 has ended
process # 36 has ended
process # 40 has ended
process # 38 has ended
process # 39 has ended
process # 37 has ended
process # 44 has ended
process # 42 has ended
process # 46 has ended
process # 45 has ended
process # 41 has ended
process # 43 has ended
process # 47 has ended
process # 50 has ended
process # 48 has ended
process # 49 has ended

2 个答案:

答案 0 :(得分:0)

  

以下测试显示异步接收请求,异步响应,但首先收到所有请求,然后回复...

目前尚不清楚这里的惊喜是什么。首先收到请求然后回复?在收到请求之前回复请求是没有意义的。

由于这是一个非常普遍的问题,因此我可能会添加一些常规信息。首先,异步并不一定意味着乱序,如果这是你所假设的。操作可以异步处理,按顺序或乱序处理,这与使用的并发模型无关。

现在,一般来说,每个返回值的函数(除非该值是一个promise)必然会同步工作,因为它不能返回它没有的任何东西。从这个意义上说,承诺本身实际上是同步创建和返回的。异步完成的是承诺的解决方案。

每个接受回调的函数通常都是异步工作的,但它并不是必须的。回调可以同步调用,有时候就像回调数组和字符串的.map()方法一样。每个返回promise的函数都应该异步工作,但也可以在返回之前同步解析promise。

重点是同步和异步是与执行顺序完全不同的,它还取决于你是使用promises还是继续传递样式。

答案 1 :(得分:0)

对于遇到这种行为并发现它好奇的其他任何人......这种情况正在发生,因为请求进入的速度非常快,以至于NodeJS在其事件循环的轮询阶段被“卡住”

以下是与Event Loop and the Polling phase相关的一些NodeJS文档。

链接中的相关信息:“如果轮询队列不为空,则事件循环将遍历执行它们的回调队列 同步 直到队列耗尽,或者达到系统相关的硬限制。“