Objective Sea

Sailing on the Objective-C sea, see?
Written by Cory, from Davander Mobile.
@corydmc on twitter, or email me objectivesea@davander.com

Node.js as an HTTP debug server

A quick node.js script to do simple http debugging. I’m working on a RESTful service, and while I’m stubbing out the client, it’s helping to have a server that’ll echo any requests back to it. This can be quickly modified to respond differently to specific requests. See the commented out block for sending 301 redirects for an example.

gist here

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/');