Merge branch 'fix-config'
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 · parents 3c8c7af, 8d2dffe · Browse files · 2 files·+59 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+59 added−32 removed
- tests:+42 added−1 removed
- other:+17 added−31 removed
Provenance
- Commit
baae446- Tree
e678b79- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
3c8c7af- Parent 2
8d2dffe
2 files changed+59 added−32 removed
modified:lib/express/server.js+17 added-31 removed
31 unmodified lines32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 connect.Server.call(this, middleware || []);35 36+ // Set "env" to {EXPRESS,CONNECT}_ENV37+ this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);38+39 // Expose objects to each other40 this.use(function(req, res, next){41 req.params = req.params || {};42 req.params.get = {};11 unmodified lines54 next();55 });56 57 // Use router, expose as app.get(), etc55- this.router = router(function(app){ self.routes = app; });58+ var fn = router(function(app){ self.routes = app; });59+ this.__defineGetter__('router', function(){60+ this.__usedRouter = true;61+ return fn;62+ });63 };64 65 /**66 * Inherit from `connect.Server`.14 unmodified lines81 if (!this.set('home')) {82 this.set('home', this.route || '/');83 }84 78- // Set "env" to {EXPRESS,CONNECT}_ENV79- this.set('env', process.env.EXPRESS_ENV || process.connectEnv.name);80- this.runConfig('any', this.set('env'));81-85 // Setup view reloading86 if (this.set('reload views')) {87 view.watcher.call(this, this.set('reload views'));88 }1 unmodified line90 connect.Server.prototype.listen.call(this, port, host);91 };92 93 /**91- * Run config callbacks for the given environment(s);92- *93- * @param {String} env ...94- * @return {Server} for chaining95- * @api private96- */97-98-Server.prototype.runConfig = function(){99- for (var i = 0, len = arguments.length; i < len; ++i) {100- var env = arguments[i];101- if (env in this.config) {102- var config = this.config[env];103- config.forEach(function(fn){104- fn.call(this);105- }, this);106- }107- }108- return this;109-};110-111-/**94 * Register the given view helpers `obj`. This method95 * can be called several times to apply additional helpers.96 *97 * @param {Object} obj91 unmodified lines189 return this;190 };191 192 /**211- * Disable `setting`.193+ * Configure callback for the given `env`.194 *213- * @param {String} setting195+ * @param {String} env196+ * @param {Function} fn197 * @return {Server} for chaining198 * @api public199 */200 201 Server.prototype.configure = function(env, fn){219- if (typeof env === 'function') fn = env, env = 'any';220- (this.config[env] = this.config[env] || []).push(fn);202+ if (typeof env === 'function') {203+ fn = env, env = 'all';204+ }205+ if (env === 'all' || this.set('env') === env) {206+ fn.call(this);207+ }208 return this;209 };210 211 // Generate routing methods1 unmodified line213 (function(method){214 Server.prototype[method] = function(path, fn){215 if (!this.__usedRouter) {216 this.use(this.router);230- this.__usedRouter = true;217 }218 this.routes[method](path, fn);219 return this;220 };2 unmodified linesmodified:test/express.test.js+42 added-1 removed
156 unmodified lines157 },158 159 'test #configure()': function(assert, beforeExit){160 var calls = [];161- var server = express.createServer();161 process.env.EXPRESS_ENV = 'development';162+ var server = express.createServer();163 164 // Config blocks165 var ret = server.configure(function(){166 assert.equal(this, server, 'Test context of configure() is the server');14 unmodified lines181 assert.eql(['any', 'dev'], calls);182 });183 },184 185+ 'test #configure() immediate call': function(assert){186+ var app = express.createServer();187+188+ app.configure(function(){189+ app.use(connect.bodyDecoder());190+ });191+ 192+ app.post('/', function(req, res){193+ res.send(req.param('name') || 'nope');194+ });195+196+ assert.response(app,197+ { url: '/', method: 'POST', data: 'name=tj', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},198+ { body: 'tj' });199+ },200+201+ 'test #configure() precedence': function(assert){202+ var app = express.createServer();203+ 204+ app.configure(function(){205+ app.use(function(req, res, next){206+ res.writeHead(200, {});207+ res.write('first');208+ next();209+ });210+ app.use(app.router);211+ app.use(function(req, res, next){212+ res.end('last');213+ });214+ });215+ 216+ app.get('/', function(req, res, params, next){217+ res.write(' route ');218+ next();219+ });220+ 221+ assert.response(app,222+ { url: '/' },223+ { body: 'first route last' });224+ },225+ 226 'test #set()': function(assert){227 var app = express.createServer();228 var ret = app.set('title', 'My App').set('something', 'else');229 assert.equal(app, ret, 'Test #set() returns server for chaining');73 unmodified lines