Akshat Akshat - 3 months ago 5
Node.js Question

Add intentional latency in express

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/

Im calling the

http://localhost:3000/wines
via ajax (the content doesn't matter). But I want to test latency. Can I do something like make express respond after 2 seconds? (I want to simulate the ajax loader and I'm running on localhost so my latency is pretty much nil)

Answer

Just call res.send inside of a setTimeout:

setTimeout((function() {res.send(items)}), 2000);