Add intentional latency in express

后端 未结 7 2078
暖寄归人
暖寄归人 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:25

    app.get('/fakeDelay', function(req,res){
        let ms = req.query.t;
        ms = (ms>5000 || isNaN(ms)) ? 1000 : parseInt(ms); 
        setTimeout((()=> res.status(200).send({delay:ms})), ms);
    })
    

    Then request the URL as: http://localhost/fakeDelay/?t=2000

    (max 5000ms and default of 1000ms on this example)

提交回复
热议问题