mirror of
https://github.com/seejohnrun/haste-server
synced 2025-07-16 10:03:45 -07:00
More work on the server
This commit is contained in:
parent
b35c89a472
commit
6bac45efe2
4 changed files with 112 additions and 27 deletions
86
server.js
86
server.js
|
@ -3,8 +3,14 @@ var fs = require('fs');
|
|||
var path = require('path');
|
||||
var url = require('url');
|
||||
|
||||
var winston = require('winston');
|
||||
|
||||
|
||||
/////////////
|
||||
// Configure loggin
|
||||
winston.remove(winston.transports.Console);
|
||||
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });
|
||||
|
||||
// TODO logging
|
||||
// TODO preparse static instead of using exists
|
||||
// TODO split into files
|
||||
// TODO only parse url once for static files
|
||||
|
@ -21,7 +27,10 @@ StaticHandler.contentTypeFor = function(ext) {
|
|||
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);
|
||||
else {
|
||||
winston.error('unable to determine content type for static asset with extension: ' + ext);
|
||||
return 'text/plain';
|
||||
}
|
||||
};
|
||||
|
||||
StaticHandler.prototype.handle = function(request, response) {
|
||||
|
@ -31,10 +40,9 @@ StaticHandler.prototype.handle = function(request, response) {
|
|||
if (exists) {
|
||||
fs.readFile(filePath, function(error, content) {
|
||||
if (error) {
|
||||
// TODO make nice
|
||||
console.log(error);
|
||||
response.writeHead(500);
|
||||
response.end();
|
||||
winston.error('unable to read file', { path: filePath, error: error.message });
|
||||
response.writeHead(500, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
|
||||
}
|
||||
else {
|
||||
response.writeHead(200, { 'content-type': StaticHandler.contentTypeFor(path.extname(filePath)) });
|
||||
|
@ -43,9 +51,9 @@ StaticHandler.prototype.handle = function(request, response) {
|
|||
});
|
||||
}
|
||||
else {
|
||||
// TODO make nice
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
winston.warn('file not found', { path: filePath });
|
||||
response.writeHead(404, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'file not found' }));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -58,38 +66,62 @@ var DocumentHandler = function() {
|
|||
|
||||
};
|
||||
|
||||
DocumentHandler.prototype.handle = function(request, response) {
|
||||
if (request.method == 'GET') {
|
||||
|
||||
DocumentHandler.prototype.handleGet = function(key, response) {
|
||||
if (documents[key]) {
|
||||
winston.verbose('retrieved document', { key: key });
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ data: documents[key] }));
|
||||
}
|
||||
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 }));
|
||||
});
|
||||
else {
|
||||
winston.warn('document not found', { key: key });
|
||||
response.writeHead(400, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'document not found' }));
|
||||
}
|
||||
};
|
||||
|
||||
DocumentHandler.prototype.handlePost = function(request, response) {
|
||||
var key = '123';
|
||||
request.on('data', function(data) {
|
||||
if (!documents[key]) {
|
||||
documents[key] = '';
|
||||
}
|
||||
documents[key] += data.toString();
|
||||
});
|
||||
request.on('end', function(end) {
|
||||
winston.verbose('added document', { key: key });
|
||||
response.end(JSON.stringify({ uuid: key }));
|
||||
});
|
||||
request.on('error', function(error) {
|
||||
// TODO handle error
|
||||
// TODO rename key to uuid everywhere behind the scenes
|
||||
// TODO finish all TODOs
|
||||
});
|
||||
};
|
||||
|
||||
///////////
|
||||
|
||||
http.createServer(function(request, response) {
|
||||
|
||||
var incoming = url.parse(request.url, false);
|
||||
|
||||
var handler = null;
|
||||
if (incoming.pathname.indexOf('/documents') === 0) {
|
||||
var match;
|
||||
|
||||
// Looking to add a new doc
|
||||
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
|
||||
handler = new DocumentHandler();
|
||||
handler.handlePost(request, response);
|
||||
}
|
||||
|
||||
// Looking up a doc
|
||||
else if ((match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/)) && request.method == 'GET') {
|
||||
handler = new DocumentHandler();
|
||||
handler.handleGet(match[1], response);
|
||||
}
|
||||
|
||||
// Otherwise, look for static file
|
||||
else {
|
||||
handler = new StaticHandler('./static');
|
||||
handler.handle(request, response);
|
||||
}
|
||||
|
||||
handler.handle(request, response);
|
||||
|
||||
}).listen(7777);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue