Skip to content

Instantly share code, notes, and snippets.

@coryalder
Created September 6, 2011 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryalder/1198525 to your computer and use it in GitHub Desktop.
Save coryalder/1198525 to your computer and use it in GitHub Desktop.
A quick node.js HTTP debug server
// associated blog post here: http://objectivesea.tumblr.com/post/9883454476/node-js-as-an-http-debug-server
var http = require('http');
var echoStore = ''; // put initial value here, will be replaced with the contents of any put or post request.
var server = http.createServer(function (request, response) {
var data = '';
request.on('data', function (chunk) {
data += chunk;
});
request.on('end', function() {
console.log("recieved http " + request.method + " request " + request.url);
if (request.method != "GET") {
echoStore = data; // save the data.
}
});
/*
// on post or put, redirect to a GET of the same resource (this is RESTful behaviour)
if (request.method == "POST" || request.method == "PUT") {
console.log("Post request, sending redirect in response");
response.writeHead(301, {'Location': request.url});
response.end('');
return;
}
*/
response.writeHead(200, {'Content-Type': 'application/json'}); // change this if your service outputs xml or html.
response.end(echoStore);
}).listen(3030, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3030/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment