var path = require('path'); var fs = require('fs'); var winston = require('winston'); // For serving static assets var StaticHandler = function(path, cacheAssets) { this.basePath = path; this.defaultPath = '/index.html'; this.cacheAssets = cacheAssets; // 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; } }; StaticHandler.cache = {}; // 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 StaticHandler.prototype.handle = function(incPath, response) { // Go to index if not found or / if (!this.availablePaths[incPath]) incPath = this.defaultPath; var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); // And then stream the file back - either from the cache or from source var cached = this.cacheAssets && this.isCached(filePath); var method = cached ? this.serveCached : this.retrieve; // Run! var _this = this; method(filePath, function(error, content) { // Get the content if (content) { var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); response.writeHead(200, { 'content-type': contentType }); response.end(content, 'utf-8'); // Stick it in the cache if its not in there if (!cached && _this.cacheAssets) { StaticHandler.cache[filePath] = content; } } // Or break down and cry else { winston.error('unable to read file', { path: filePath, error: error }); response.writeHead(500, { 'content-type': 'application/json' }); response.end(JSON.stringify({ message: 'IO: Unable to read file' })); // If it was cached, bust the cache if (cached) { StaticHandler.cache[filePath] = null; } } }); }; // Retrieve from the file StaticHandler.prototype.retrieve = function(filePath, callback) { var _this = this; winston.verbose('loading static asset', { path: filePath }); fs.readFile(filePath, function(error, content) { callback(error, content); }); }; // Retrieve from memory cache StaticHandler.prototype.serveCached = function(filePath, callback) { callback(undefined, StaticHandler.cache[filePath]); }; // Determine if a given filePath is cached or not StaticHandler.prototype.isCached = function(filePath) { return !!StaticHandler.cache[filePath]; }; module.exports = StaticHandler;