Skip to content

Instantly share code, notes, and snippets.

@onteria
Created June 2, 2011 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save onteria/1005599 to your computer and use it in GitHub Desktop.
Save onteria/1005599 to your computer and use it in GitHub Desktop.
using Object.create()
var fs = require('fs');
var http = require('http');
var events = require('events');
function MyServer(config_file) {
var server = Object.create(new events.EventEmitter);
server.config = {};
server.StartServer = function() {
http.createServer((function (req, res) {
this.SendResponse(res);
}).bind(this)).listen(this.config.port, this.config.host, function() {
console.log("Server is bound.")
});
};
server.SendResponse = function(response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
};
server.ParseConfig = function(err,json_string) {
if(err) {
console.error("Could not open config file: ", err);
process.exit(1);
}
try {
this.config = JSON.parse(json_string);
this.StartServer();
}
catch(exception) {
console.error("There was an error parsing the json config file: ", exception);
process.exit(1);
}
};
server.SetupServer = function() {
fs.readFile(config_file, 'utf8', this.ParseConfig.bind(this) );
}
return server;
}
var serverObj = MyServer('config.json');
serverObj.SetupServer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment