2011-11-18 20:51:38 +00:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
|
|
|
// For serving static assets
|
|
|
|
|
|
|
|
var StaticHandler = function(path) {
|
2011-11-18 21:00:05 +00:00
|
|
|
this.basePath = path;
|
2011-11-18 20:51:38 +00:00
|
|
|
this.defaultPath = '/index.html';
|
2011-11-18 22:36:35 +00:00
|
|
|
// Grab the list of available files - and move into hash for quick lookup
|
|
|
|
var available = fs.readdirSync(this.basePath);
|
|
|
|
this.availablePaths = {};
|
|
|
|
for (var i = 0; i < available.length; i++) {
|
|
|
|
this.availablePaths['/' + available[i]] = true;
|
|
|
|
}
|
2011-11-18 20:51:38 +00:00
|
|
|
};
|
|
|
|
|
2011-11-22 03:19:43 +00:00
|
|
|
StaticHandler.cache = {};
|
|
|
|
|
2011-11-18 20:51:38 +00:00
|
|
|
// Determine the content type for a given extension
|
|
|
|
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 {
|
|
|
|
winston.error('unable to determine content type for static asset with extension: ' + ext);
|
|
|
|
return 'text/plain';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle a request, and serve back the asset if it exists
|
2011-11-18 21:00:05 +00:00
|
|
|
StaticHandler.prototype.handle = function(incPath, response) {
|
2011-11-18 22:36:35 +00:00
|
|
|
// Go to index if not found or /
|
|
|
|
if (!this.availablePaths[incPath]) incPath = this.defaultPath;
|
2011-11-18 21:00:05 +00:00
|
|
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
|
2011-11-18 22:36:35 +00:00
|
|
|
// And then stream the file back
|
2011-11-22 03:19:43 +00:00
|
|
|
if (StaticHandler.cache[filePath]) {
|
|
|
|
this.serveCached(filePath, response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.retrieve(filePath, response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Retrieve from the file
|
|
|
|
StaticHandler.prototype.retrieve = function(filePath, response) {
|
2011-11-18 21:22:00 +00:00
|
|
|
var _this = this;
|
2011-11-18 22:36:35 +00:00
|
|
|
fs.readFile(filePath, function(error, content) {
|
|
|
|
if (error) {
|
|
|
|
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' }));
|
2011-11-18 20:51:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-11-18 22:36:35 +00:00
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
|
|
|
|
response.writeHead(200, { 'content-type': contentType });
|
|
|
|
response.end(content, 'utf-8');
|
2011-11-22 03:19:43 +00:00
|
|
|
// Stick it in the cache
|
|
|
|
StaticHandler.cache[filePath] = content;
|
2011-11-18 20:51:38 +00:00
|
|
|
}
|
2011-11-18 22:36:35 +00:00
|
|
|
});
|
2011-11-18 20:51:38 +00:00
|
|
|
};
|
|
|
|
|
2011-11-22 03:19:43 +00:00
|
|
|
// Retrieve from memory cache
|
|
|
|
StaticHandler.prototype.serveCached = function(filePath, response) {
|
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
|
|
|
|
response.writeHead(200, { 'content-type': contentType });
|
|
|
|
response.end(StaticHandler.cache[filePath], 'utf-8');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-18 20:51:38 +00:00
|
|
|
module.exports = StaticHandler;
|