Created
August 21, 2021 18:32
-
-
Save arastu/ae22af498a6d0cdbe35960320381fe0b to your computer and use it in GitHub Desktop.
Node.js http server with random responses time for testing purposes
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
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