
var http = require('http');

function writeOutputForm(response, code) {
	var fs = require('fs');

	fs.readFile('./form.html',function (err, form) {
		if (err) {
			throw err;
		}

		var html = form.toString();

		if (code) {
			html.replace("{code}", code);
		} else {
			html.replace("{code}", "JSON.stringify(api);");
		}

		response.write(html);

		response.end();
	});
}

var server = http.createServer();

server.on('request', function(request, response) {
	console.log('Got a hit.');

	response.writeHead(200, "OK", {'Content-Type': 'text/html'});

	switch (request.method + request.url) {
		case 'GET/':
			writeOutputForm(response, null);
			break;

		case 'POST/run':
			var reply = '';

			request.on('data', function(chunk) {
				console.log("Received body data: " + chunk.toString());

				reply += chunk.toString();
			});

			request.on('end', function() {
				try {
					var queryString = require('querystring');

					var concierge = require('./concierge.js');
					var runner = require("./runner.js");

					concierge.__fixup(request,response);

					codeToRun = queryString.parse(reply).code.trim();

					var result = runner.run(concierge, codeToRun);

					response.write(result);
				} catch (err) {
					response.write("Unknown error.");
				}

				writeOutputForm(response, reply);
			});

			break;

		default:
			response.writeHead(301, {'Location': '/'});
			response.end("Nothing to see here. Move along. Nothing to see.");
			break;
	}
});

server.listen(8000, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8000/');

@@ -218,24 +218,31 @@
 (code) %7B%0A%09%09%09
+html = 
 html.replace
@@ -269,24 +269,31 @@
 %7D else %7B%0A%09%09%09
+html = 
 html.replace
@@ -703,16 +703,34 @@
 ly = '';
+%0A%09%09%09var code = '';
 %0A%0A%09%09%09req
@@ -1098,21 +1098,16 @@
 %09%09%09%09code
-ToRun
  = query
@@ -1188,13 +1188,8 @@
 code
-ToRun
 );%0A%0A
@@ -1313,21 +1313,20 @@
 sponse, 
-reply
+code
 );%0A%09%09%09%7D)

Fix field replacement in code form.