Skip to content

Instantly share code, notes, and snippets.

@issa-tseng
Created August 14, 2019 23:41
Show Gist options
  • Save issa-tseng/97f937978f98ce6c723ea47c945e5b0b to your computer and use it in GitHub Desktop.
Save issa-tseng/97f937978f98ce6c723ea47c945e5b0b to your computer and use it in GitHub Desktop.
a really dumb quick node.js proxy to introduce artificial latency to a local port
// node latent.js fromport toport delay
// where fromport is the port of the service and toport is the port to serve the proxy on
const { createServer, request } = require('http');
const [ , , fromport, toport, delaystr = 1000 ] = process.argv;
const delay = parseInt(delaystr);
createServer((req, res) => {
setTimeout(() => {
const prox = request({
hostname: 'localhost',
port: fromport,
path: req.url,
method: req.method,
headers: req.headers
}, (pres) => {
res.writeHead(pres.statusCode, pres.headers);
pres.pipe(res, { end: true });
});
req.pipe(prox, { end: true });
}, delay);
}).listen(toport);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment