Skip to content

Instantly share code, notes, and snippets.

@onteria
Created June 2, 2011 22:43
Show Gist options
  • Save onteria/1005507 to your computer and use it in GitHub Desktop.
Save onteria/1005507 to your computer and use it in GitHub Desktop.
Using .bind
var fs = require('fs');
var http = require('http');
function MyServer(config_file) {
this.config = {};
this.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.")
});
};
this.SendResponse = function(response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
};
this.ParseConfig = function(err,json_string) {
if(err) {
console.error("Could not open config file: ", err);
process.exit(1);
}
try {
console.log(json_string);
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);
}
};
fs.readFile(config_file, 'utf8', (function(err,json_string) { this.ParseConfig(err,json_string); }).bind(this) );
}
var server = new MyServer('config.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment