haste-server/server.js

94 lines
2.1 KiB
JavaScript
Raw Normal View History

2011-11-18 16:17:41 +01:00
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
// TODO logging
// TODO preparse static instead of using exists
//////////////
var StaticHandler = function(path) {
this.path = path;
this.defaultPath = '/index.html';
};
StaticHandler.contentTypeFor = function(ext) {
if (ext == '.js') return 'text/javascript';
else if (ext == '.css') return 'text/css';
else if (ext == '.html') return 'text/html';
else if (ext == '.ico') return 'image/ico';
else console.log(ext);
};
StaticHandler.prototype.handle = function(request, response) {
var inc = url.parse(request.url, false);
var filePath = this.path + (inc.pathname == '/' ? this.defaultPath : inc.pathname);
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
// TODO make nice
console.log(error);
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'content-type': StaticHandler.contentTypeFor(path.extname(filePath)) });
response.end(content, 'utf-8');
}
});
}
else {
// TODO make nice
response.writeHead(404);
response.end();
}
});
};
///////////
2011-11-18 16:49:00 +01:00
var documents = {};
var DocumentHandler = function() {
};
DocumentHandler.prototype.handle = function(request, response) {
if (request.method == 'GET') {
}
else if (request.method == 'POST') {
var key = '123';
request.on('data', function(data) {
if (!documents[key]) {
documents[key] = '';
}
documents[key] += data.toString();
});
request.on('end', function(end) {
response.end(JSON.stringify({ uuid: key }));
});
}
};
///////////
2011-11-18 16:17:41 +01:00
http.createServer(function(request, response) {
2011-11-18 16:49:00 +01:00
var incoming = url.parse(request.url, false);
2011-11-18 16:49:38 +01:00
var handler = null;
2011-11-18 16:49:00 +01:00
if (incoming.pathname.indexOf('/documents') === 0) {
2011-11-18 16:49:38 +01:00
handler = new DocumentHandler();
2011-11-18 16:49:00 +01:00
}
else {
2011-11-18 16:49:38 +01:00
handler = new StaticHandler('./static');
2011-11-18 16:49:00 +01:00
}
2011-11-18 16:17:41 +01:00
2011-11-18 16:49:38 +01:00
handler.handle(request, response);
2011-11-18 16:17:41 +01:00
}).listen(7777);