haste-server/lib/document_handler.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2011-11-18 21:51:38 +01:00
var winston = require('winston');
// For handling serving stored documents
var DocumentHandler = function() {
};
// TODO implement with FS backend
DocumentHandler.documents = {};
DocumentHandler.prototype.handleGet = function(key, response) {
if (DocumentHandler.documents[key]) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: DocumentHandler.documents[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 (!DocumentHandler.documents[key]) {
2011-11-18 22:28:09 +01:00
response.writeHead(200, { 'content-type': 'application/json' });
2011-11-18 21:51:38 +01:00
DocumentHandler.documents[key] = '';
}
DocumentHandler.documents[key] += data.toString();
});
request.on('end', function(end) {
winston.verbose('added document', { key: key });
2011-11-18 22:25:18 +01:00
response.end(JSON.stringify({ key: key }));
2011-11-18 21:51:38 +01:00
});
request.on('error', function(error) {
2011-11-18 22:28:09 +01:00
winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'connection error' }));
2011-11-18 21:51:38 +01:00
});
};
2011-11-18 22:22:00 +01:00
// TODO block modifying
2011-11-18 21:51:38 +01:00
module.exports = DocumentHandler;