Skip to content

Instantly share code, notes, and snippets.

@HoldYourWaffle
Last active September 15, 2019 20:39
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 HoldYourWaffle/6812334c4fd4c21b4b8b2bb1a15a9bb2 to your computer and use it in GitHub Desktop.
Save HoldYourWaffle/6812334c4fd4c21b4b8b2bb1a15a9bb2 to your computer and use it in GitHub Desktop.
Simple node echo http server
// FIXME this leaks memory for some reason
const http = require('http');
const port = parseInt(process.argv[2]);
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.write(stringify(req));
res.end();
});
server.listen(port, err => {
if (err) {
throw err;
} else {
console.log(`Echo chamber is listening on ${port}`);
}
});
function stringify(data) { // https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format
const cache = [];
return JSON.stringify(data, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return '[Circular]';
}
cache.push(value);
}
return value;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment