Skip to content

Instantly share code, notes, and snippets.

@jduhls
Last active October 28, 2017 15:36
Show Gist options
  • Save jduhls/21d3619ec3c6f215b22b to your computer and use it in GitHub Desktop.
Save jduhls/21d3619ec3c6f215b22b to your computer and use it in GitHub Desktop.
nodejs randomly long request simulator
var http = require('http');
//Select the port on which to run this delayed request server:
var port = 8080;
//Select the maximum number of seconds to delay requests to this server:
var seconds = 1440; //Iknowright?!?! I needed a delay of up to 24 minutes!
//Gets random delay time in ms (maximum of var seconds):
var timeout = Math.floor((Math.random() * seconds) + 1) * 1000;
var server = http.createServer(function (req, res) {
//IMPORTANT! Override nodejs default 2 minute connection timeout - never timeout connections:
res.connection.setTimeout(0);
setTimeout((function() {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({"message":"Sorry for the " + Math.floor(timeout/1000) + " second wait!"}));
}), timeout);
});
server.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment