Skip to content

Instantly share code, notes, and snippets.

@arastu
Created August 21, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arastu/ae22af498a6d0cdbe35960320381fe0b to your computer and use it in GitHub Desktop.
Save arastu/ae22af498a6d0cdbe35960320381fe0b to your computer and use it in GitHub Desktop.
Node.js http server with random responses time for testing purposes
const http = require('http');
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomIDGenerator(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const requestListener = function (req, res) {
setTimeout(() => {
res.writeHead(200);
res.end(randomIDGenerator(32));
}, randomInt(1000, 10000));
}
const server = http.createServer(requestListener);
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment