Simulate upstream proxy timeout using nodejs

This is something that I have came across while tuning an nginx server which has multiple tomcat instances as upstream. We were trying to adjust the read timeout of the upstream proxies. It is hard to simulate this by stopping the backend as it will throw a 503 bad gateway. So, for simulating this, we used a nodejs script.


/*server.js*/
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
function test(res) {
console.log("Inside test");
}
const server = http.createServer(function(req, res) {
console.log("Starting");
setTimeout(test, 20000);
});

server.listen(port, hostname, function() {
console.log('Server running at http://'+ hostname + ':' + port + '/');
});