Created
September 6, 2011 18:26
-
-
Save coryalder/1198525 to your computer and use it in GitHub Desktop.
A quick node.js HTTP debug 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
// 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