function handleStatic (root, prefix) { if (! root) { throw new Error('missing root directory for static files'); } if (! prefix) { prefix = ''; } prefix = prefix.replace(/([^\w])/g, '\\$1'); var realRoot = fs.realpath(root), realPaths = {}, WebApp = this, notFound = function (response) { return function (err) { response.notFound('text/plain'); return 'not found'; }; }; proton.beforeStart(WebApp, realRoot.then( function (root) { WebApp.get(new RegExp('^' + prefix + '(.+\\.(css|js|jpe?g|gif|png|swf|html))$'), function (request, response, path, type) { var path = root + path; if (! (path in realPaths)) { realPaths[path] = fs.realpath(path); } return realPaths[path].then( function (assetPath) { return fs.readFile(assetPath).then( function (contents) { response.setStatus(200); response.setType(types[type]); return contents; }, notFound(response) ); }, notFound(response) ); }); }, function (err) { throw new Error('cannot add static handler for "' + root + '": ' + err.message); } )); };
WebApp.get(new RegExp('^' + prefix + '(.+\\.(css|js|jpe?g|gif|png|swf|html))$'), function (request, response, path, type) { var path = root + path; if (! (path in realPaths)) { realPaths[path] = fs.realpath(path); } return realPaths[path].then( function (assetPath) { return fs.readFile(assetPath).then( function (contents) { response.setStatus(200); response.setType(types[type]); return contents; }, notFound(response) ); }, notFound(response) ); });
var fs = require('promised-io/lib/fs'), promise = require('promised-io/lib/promise'), sys = require('sys'), proton = require('proton'), spectrum = require('spectrum'); var getRenderer = fs.realpath(__dirname + '/../views').then(function (path) { return new spectrum.Renderer(path); }); var types = { 'png' : 'image/png', 'jpg' : 'image/jpeg', 'jpeg' : 'image/jpeg', 'gif' : 'image/gif', 'css' : 'text/css', 'js' : 'text/javascript', 'swf' : 'application/x-shockwave-flash', 'html' : 'text/html' }; var Response = function (webapp) { this.webapp = webapp; this._response = { status: 404, headers: { 'Content-Type' : 'text/plain' } }; this._handled = false; };