Skip to content

Instantly share code, notes, and snippets.

@lyle
Last active December 20, 2015 18:28
Show Gist options
  • Save lyle/6175829 to your computer and use it in GitHub Desktop.
Save lyle/6175829 to your computer and use it in GitHub Desktop.
A node server that will delay it's response by 1000 milliseconds, or ?delay=milliseconds
var url = require('url');
var http = require('http');
http.createServer(function (req, res) {
var startTime = Date.now();
var delay = url.parse(req.url, true).query.delay || 1000;
setTimeout(function(){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('We tried to delay your request by '.concat(
delay,
' milliseconds.\n',
'In reality it took ',
(Date.now() - startTime),
' milliseconds.\n'
));
},delay)
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
console.log('To get a 3 seconds delay: http://127.0.0.1:1337/?delay=3000');
@lyle
Copy link
Author

lyle commented Aug 7, 2013

To run this via node simply download the index.js file and from a command line:

node index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment