Skip to content
Myr

Added view reloading

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 95e6fff · Browse files · 3 files+79 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+79 added−2 removed

  • other:+79 added−2 removed

3 files+79 added−2 removed

modified:examples/jade/app.js+11 added
modified:lib/express/server.js+7 added
9 unmodified lines
10 */1112var sys = require('sys'),13 url = require('url'),14 view = require('./view'),15 connect = require('connect'),16 queryString = require('querystring'),17 router = require('connect/middleware/router');18
48 unmodified lines
6768Server.prototype.listen = function(port){69 this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);70 this.runConfig('any', this.set('env'));7172 // Setup view reloading73 if (this.set('view reloading')) {74 view.watcher.call(this, this.set('view reloading'));75 }76 77 connect.Server.prototype.listen.call(this, port);78};7980/**
85 unmodified lines
modified:lib/express/view.js+61 added-2 removed
11 unmodified lines
12var extname = require('path').extname,13 mime = require('connect/utils').mime,14 utils = require('connect/utils'),15 http = require('http'),16 sys = require('sys'),17 fs = require('fs');1819/**20 * Cache supported template engine exports to
23 unmodified lines
44 viewCache = {};45};4647/**48 * Cache view at the given `path`.49 *50 * @param {String} padth51 * @return {String}52 * @api private53 */5455function cacheView(path) {56 return viewCache[path] = fs.readFileSync(path, 'utf8');57}5859/**60 * Return view root path for the given `app`.61 *62 * @param {express.Server} app63 * @return {String}64 * @api private65 */6667function viewRoot(app) {68 return app.set('views') || process.cwd() + '/views';69}7071/**72 * Setup view reloading, checking at the given `interval`73 * which defaults to 5 minutes.74 *75 * @param {Object} options76 * @api private77 */7879exports.watcher = function(options){80 options = options || { interval: 300000 };81 (function cache(dir){82 fs.readdir(dir, function(err, files){83 if (!err) {84 files.forEach(function(file){85 file = dir + '/' + file;86 fs.stat(file, function(err, stats){87 if (!err) {88 if (stats.isFile()) {89 fs.watchFile(file, options, function(curr, prev){90 if (curr.mtime > prev.mtime) {91 cacheView(file);92 }93 });94 } else if (stats.isDirectory()) {95 cache(file);96 }97 }98 });99 });100 }101 });102 })(viewRoot(this));103};104105/**106 * Render `view` partial with the given `options`.107 *108 * Options:109 * - `as` Variable name for each `collection` value, defaults to the view name.
70 unmodified lines
180 options = options || {};181182 // Defaults183 var self = this,184 root = viewRoot(this.app),185 ext = extname(view),186 partial = options.partial,127 root = options.root || this.app.set('views') || process.cwd() + '/views',187 layout = options.layout === undefined ? true : options.layout,188 layout = layout === true189 ? 'layout' + ext190 : layout;
38 unmodified lines
229 }230231 // Cache contents232 try {174 var str = viewCache[path] || (viewCache[path] = fs.readFileSync(path, 'utf8'));233 var str = viewCache[path] || cacheView(path);234 } catch (err) {235 return error(err);236 }237
23 unmodified lines