Add intentional latency in express

后端 未结 7 2068
暖寄归人
暖寄归人 2020-12-29 18:37

Im using express with node.js, and testing certain routes. I\'m doing this tute at http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

7条回答
  •  情话喂你
    2020-12-29 19:14

    To apply it globaly on all requests you can use the following code:

    app.use( ( req, res, next ) => {
        setTimeout(next, Math.floor( ( Math.random() * 2000 ) + 100 ) );
    });
    

    Time values are:

    Max = 2000 (sort of.... min value is added so in reality its 2100)

    Min = 100

提交回复
热议问题