Skip to content

Instantly share code, notes, and snippets.

@noamtm
Created August 31, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noamtm/cf16ed7b8c39be1ddb45 to your computer and use it in GitHub Desktop.
Save noamtm/cf16ed7b8c39be1ddb45 to your computer and use it in GitHub Desktop.
node.js echo server
/*
Node.js echo server
Starts an http server on port 8000. The server responds with:
- request headers as json
- 3 newline characters ('\n\n\n')
- request body (piped)
It does not attempt to "understand" the request in any way.
I use it for debugging clients, mostly POSTs.
Starting point: http://stackoverflow.com/questions/17190733/simplest-nodejs-echo-server
*/
var http = require('http');
var server = http.createServer(function (req, res) {
res.write(JSON.stringify(req.headers));
res.write('\n\n\n');
req.pipe(res);
});
server.listen(8000);
/*
Can also work as a one-liner -- just copy-paste to your shell:
node -e "require('http').createServer(function (req, res) {res.write(JSON.stringify(req.headers)); res.write('\n\n\n'); req.pipe(res);}).listen(8000);"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment