Last active
October 28, 2017 15:36
-
-
Save jduhls/21d3619ec3c6f215b22b to your computer and use it in GitHub Desktop.
nodejs randomly long request simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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