Skip to content

Instantly share code, notes, and snippets.

@themasch
Created June 6, 2010 12:52
Show Gist options
  • Save themasch/427568 to your computer and use it in GitHub Desktop.
Save themasch/427568 to your computer and use it in GitHub Desktop.
var sys = require('sys'),
http = require('http'),
fs = require('fs'),
couchdb = require('./lib/couchdb'),
client = couchdb.createClient(5984, 'localhost'),
db = client.db('chat');
function chat(port) {
this.createServer(port);
}
chat.prototype = {
createServer: function(port) {
var self = this;
http.createServer(function(rq, rs) { self.handleRequest(rq, rs) }).listen(port);
sys.print("server running on port " + port + "\n");
},
handleRequest: function (req, res) {
sys.print("incoming request\n");
res.params = req.url.substr(1).split('/');
sys.p(this);
if (res.params[0] == undefined || res.params[0] == 'files') {
sys.print("Handler: file\n");
this.handleFileRequest(req, res);
}
else if (res.params[0] == 'data') {
sys.print("Handler: data\n");
this.handleDataRequest(req, res);
}
},
handleFileRequest: function(req, res) {
var file = null;
sys.print("handleFileRequest:\n");
if(res.params.length == 0) {
file = 'index.html';
}
if(res.params.length >= 2) {
file = './' + res.params.slice(1).join('/');
}
sys.print(" " + file);
res.writeHead(404);
res.end();
},
handleDataRequest: function(req, res) {
res.writeHead(404);
res.end();
}
}
new chat(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment