/*
 * node-api-factory
 * https://github.com/santthosh/node-api-factory
 *
 * Copyright (c) 2013 Santthosh
 * Licensed under the MIT license.
 */

'use strict';

var http = require('http');
var httpProxy = require('http-proxy');
var fs = require('fs');
var path = require('path');
var url = require('url');

// Use default configuration if not already provided
var config = null;
var configFile = process.cwd() + '/api-factory.json';
if(fs.existsSync(configFile)) {
    config = require(configFile);
} else {
    config = {
        "root" : "api-factory",
        "port" : 8111
    }
}

var routerBase = config.root; //You can change the root directory for the path of the routes

var server = httpProxy.createServer(function (request, response, proxy) {
    var jsHandlerFile = routerBase + url.parse(request.url).pathname + ".js";
    var jsonHandlerFile = routerBase + url.parse(request.url).pathname + ".json";
    if(fs.existsSync(jsHandlerFile)) {
        try {
            var handler = require(process.cwd() + "/" + jsHandlerFile);
            handler(request,response,proxy,config);
        } catch(err) {
            response.statusCode = 500;
            response.write("<b>500 - Internal Server Error</b><br/>"+err.message);
            response.end();
        }
    } else if(fs.existsSync(jsonHandlerFile)) {
        try {
            var responseBodyProvider = require(process.cwd() + "/" + jsonHandlerFile);
            fs.readFile(resposeBodyProvider, 'utf8', function (err,data) {
                if (err) {
                    return console.log(err);
                }
                response.setHeader("Content-Type", "application/json");
                response.write(data);
                response.end();
            });
        } catch(err) {
            response.statusCode = 500;
            response.write("<b>500 - Internal Server Error</b><br/>"+err.message);
            response.end();
        }
    }
    else {
        response.statusCode = 404;
        response.write("<b>404 - Not Found</b>");
        response.end();
    }
});

try {
    // Listen on port configured port, IP defaults to 127.0.0.1
    server.listen(config.port);

    // Put a friendly message on the terminal
    console.log("api-factory running at http://127.0.0.1:" + config.port  + "/");
} catch(err) {
    console.log(err.message);
}

@@ -1478,25 +1478,37 @@
     
-fs.readFil
+var jsonBody = requir
 e(respo
+n
 seBo
@@ -1521,250 +1521,67 @@
 ider
-, 'utf8', function (err,data) %7B%0D%0A                if (err) %7B%0D%0A                    return console.log(err);%0D%0A                %7D%0D%0A                response.setHeader(%22Content-Type%22, %22application/json%22);%0D%0A                response.write(data);%0D%0A    
+);%0D%0A            response.write(JSON.stringify(jsonBody));%0D%0A
     
@@ -1609,25 +1609,8 @@
 );%0D%0A
-            %7D);%0D%0A
     

Simplify JSON support