Created
June 2, 2011 23:51
-
-
Save onteria/1005599 to your computer and use it in GitHub Desktop.
using Object.create()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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