Skip to content

Instantly share code, notes, and snippets.

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