var http = require("http");

function HttpServer(config) {
  this.port = config.port || 8181;
  this.address = config.address || "localhost";
  this.options = {
    data: null,
    resCode: 200
  };
}

HttpServer.prototype.start = function (fn) {
  this.server = http.createServer(function (req, res) {
    res.writeHead(this.options.resCode, {"Content-Type": "application/json"});
    res.end(JSON.stringify(this.options.data));
  }.bind(this)).listen(this.port, this.address, fn);
  return this;
};

HttpServer.prototype.setup = function (data, resCode) {
  this.options.data = data;
  this.options.resCode = resCode || 200;
  return this;
};

HttpServer.prototype.on = function (event, fn) {
  this.server.on(event, fn);
};

HttpServer.prototype.stop = function (fn) {
  this.server.close();
  if (fn) {
    fn();
  }
};

exports.HttpServer = HttpServer;

var http = require("http");

function HttpServer(config) {
  this.port = config.port || 8181;
  this.address = config.address || "localhost";
  this.options = {
    data: null,
    headers: {"Content-Type": "application/json"},
    returnPayload: false,
    resCode: 200
  };
}

HttpServer.prototype.start = function (fn) {

  this.server = http.createServer();

  this.server.on("request", (req, res) => {

    if (this.options.returnPayload === true) {
      this.options.data = {
        payload: "",
        method: req.method
      };

      req.addListener("data", chunk => {
        this.options.data.payload += chunk;
      });

      req.addListener("end", chunk => {
        if (chunk) {
          this.options.data.payload += chunk;
        }
        res.writeHead(this.options.resCode, this.options.headers);
        res.end(JSON.stringify(this.options.data));
      });
    } else {
      res.writeHead(this.options.resCode, this.options.headers);
      res.end(JSON.stringify(this.options.data));
    }
  }).listen(this.port, this.address, fn);

  return this;
};

HttpServer.prototype.on = function (event, fn) {
  this.server.on(event, fn);
};

HttpServer.prototype.setup = function (data, resCode, returnPayload = false) {
  this.options.data = data;
  this.options.resCode = resCode || 200;
  this.options.returnPayload = returnPayload;

  return this;
};

HttpServer.prototype.stop = function (fn) {
  this.server.close();
  if (fn) {
    fn();
  }
};

exports.HttpServer = HttpServer;

Add option to enable response with payload
Add option to enable response with payload

This allows the server to respond with an object representing the
.payload as sent by the request, and the corresponding .method used by
the caller.
