Last active
September 15, 2019 20:39
-
-
Save HoldYourWaffle/6812334c4fd4c21b4b8b2bb1a15a9bb2 to your computer and use it in GitHub Desktop.
Simple node echo http server
This file contains 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
// 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