application.js
This file
Path
lib/application.js
563 lines · 11.7 KiB · JavaScript
Last changed by
Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
History of this fileTake it
The walk stopped after 200 commits: lines older than d1ce4c9 point at it.
req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 1–14why, one step back12/*!3 * Express - proto4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var connect = require('connect')13 , Router = require('./router')14 , methods = Router.methods.concat('del', 'all')refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba
line 15why, one step back15 , middleware = require('./middleware')new ViewTj Holowaychukbefore adoptione1da383
line 16why, one step back16 , View = require('./view')req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 17–19why, one step back17 , url = require('url')18 , utils = connect.utils19 , path = require('path')Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 20why, one step back20 , http = require('http')req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 21–33why, one step back21 , join = path.join22 , fs = require('fs')23 , qs = require('qs');2425/**26 * Expose `HTTPServer`.27 */2829var app = exports = module.exports = {};3031/**32 * Initialize the server.33 *refactoring boot logicTj Holowaychukbefore adoption8848004
lines 34–37why, one step back34 * - setup default configuration35 * - setup default middleware36 * - setup route reflection methods37 *req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 38–40why, one step back38 * @api private39 */40refactoring boot logicTj Holowaychukbefore adoption8848004
line 41why, one step back41app.init = function(){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 42why, one step back42 var self = this;app.lookup()Tj Holowaychukbefore adoption76b7d69
line 43why, one step back43 this.cache = {};req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 44–47why, one step back44 this.settings = {};45 this.engines = {};46 this.redirects = {};47 this.isCallbacks = {};app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
line 48why, one step back48 this.viewCallbacks = [];refactoring boot logicTj Holowaychukbefore adoption8848004
line 49why, one step back49 this.defaultConfiguration();req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 50why, one step back50miscTj Holowaychukbefore adoptiond4b08e9
line 51why, one step back51 // route reflection methodsrefactoring boot logicTj Holowaychukbefore adoption8848004
lines 52–53why, one step back52 methods.forEach(function(method){53 self.lookup[method] = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 54why, one step back54 return self._router.lookup(method, path);refactoring boot logicTj Holowaychukbefore adoption8848004
lines 55–57why, one step back55 };5657 self.match[method] = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 58why, one step back58 return self._router.match(method, path);refactoring boot logicTj Holowaychukbefore adoption8848004
lines 59–61why, one step back59 };6061 self.remove[method] = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 62why, one step back62 return self._router.lookup(method, path).remove();refactoring boot logicTj Holowaychukbefore adoption8848004
lines 63–80why, one step back63 };64 });6566 // del -> delete67 self.lookup.del = self.lookup.delete;68 self.match.del = self.match.delete;69 self.remove.del = self.remove.delete;70};7172/**73 * Initialize application configuration.74 *75 * @api private76 */7778app.defaultConfiguration = function(){79 var self = this;80req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 81why, one step back81 // default settingsFixed res.clearCookie(), default path to "root" settingTj Holowaychukbefore adoption0f2f3d4
line 82why, one step back82 this.set('root', '/');req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 83–84why, one step back83 this.set('env', process.env.NODE_ENV || 'development');84refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba
line 85why, one step back85 // implicit middlewarereq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 86why, one step back86 this.use(connect.query());refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba
line 87why, one step back87 this.use(middleware.init(this));req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 88why, one step back88docsTj Holowaychukbefore adoption6ae32fd
line 89why, one step back89 // app localsreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 90why, one step back90 this.locals = function(obj){misc refactoringTj Holowaychukbefore adoption4ed92d5
line 91why, one step back91 for (var key in obj) self.locals[key] = obj[key];req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 92why, one step back92 return self;app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
lines 93–94why, one step back93 };94docsTj Holowaychukbefore adoption6ae32fd
line 95why, one step back95 // response localsapp.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
line 96why, one step back96 this.locals.use = function(fn){added sync signature for app.locals.use()Tj Holowaychukbefore adoption965e1d6
lines 97–104why, one step back97 if (3 == fn.length) {98 self.viewCallbacks.push(fn);99 } else {100 self.viewCallbacks.push(function(req, res, done){101 fn(req, res);102 done();103 });104 }app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
line 105why, one step back105 return this;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 106–109why, one step back106 };107108109 // routerAdded `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
lines 110–111why, one step back110 this._router = new Router(this);111 this.routes = this._router.routes;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 112why, one step back112 this.__defineGetter__('router', function(){fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 113why, one step back113 this._usedRouter = true;Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 114why, one step back114 return self._router.middleware;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 115–134why, one step back115 });116117 // default locals118 this.locals.settings = this.settings;119120 // default production configuration121 this.configure('production', function(){122 this.enable('view cache');123 });124};125126/**127 * Remove routes matching the given `path`.128 *129 * @param {Route} path130 * @return {Boolean}131 * @api public132 */133134app.remove = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 135why, one step back135 return this._router.lookup('all', path).remove();req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 136–147why, one step back136};137138/**139 * Lookup routes defined with a path140 * equivalent to `path`.141 *142 * @param {Stirng} path143 * @return {Array}144 * @api public145 */146147app.lookup = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 148why, one step back148 return this._router.lookup('all', path);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 149–159why, one step back149};150151/**152 * Lookup routes matching the given `url`.153 *154 * @param {Stirng} url155 * @return {Array}156 * @api public157 */158159app.match = function(url){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 160why, one step back160 return this._router.match('all', url);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 161–163why, one step back161};162163/**docsTj Holowaychukbefore adoption2e22887
line 164why, one step back164 * Proxy `connect#use()` to apply settings toreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 165–167why, one step back165 * mounted applications.166 *167 * @param {String|Function|Server} routemisc refactoringTj Holowaychukbefore adoption1334a74
line 168why, one step back168 * @param {Function|Server} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 169why, one step back169 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 170–172why, one step back170 * @api public171 */172misc refactoringTj Holowaychukbefore adoption1334a74
line 173why, one step back173app.use = function(route, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 174–175why, one step back174 var app, home, handle;175misc refactoringTj Holowaychukbefore adoption1334a74
line 176why, one step back176 if ('string' != typeof route) fn = route, route = '/';req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 177–178why, one step back177178 // express appmisc refactoringTj Holowaychukbefore adoption1334a74
line 179why, one step back179 if (fn.handle && fn.set) app = fn;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 180–183why, one step back180181 // restore .app property on req and res182 if (app) {183 app.route = route;misc refactoringTj Holowaychukbefore adoption1334a74
line 184why, one step back184 fn = function(req, res, next) {req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 185–192why, one step back185 var orig = req.app;186 app.handle(req, res, function(err){187 req.app = res.app = orig;188 next(err);189 });190 };191 }192misc refactoringTj Holowaychukbefore adoption1334a74
line 193why, one step back193 connect.proto.use.call(this, route, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 194–197why, one step back194195 // mounted an app, invoke the hook196 // and adjust some settings197 if (app) {tweak res.redirect() mount route logicTj Holowaychukbefore adoption43d9f6e
lines 198–201why, one step back198 var root = this.get('root') || this.route;199 if ('/' == root) root = '';200 root = root + (app.get('root') || app.route);201 app.set('root', root);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 202why, one step back202 app.parent = this;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 203why, one step back203 app.emit('mount', this);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 204–213why, one step back204 }205206 return this;207};208209/**210 * Register the given template engine callback `fn`211 * as `ext`. For example we may wish to map ".html"212 * files to ejs rather than using the ".ejs" extension.213 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 214why, one step back214 * app.engine('.html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 215–217why, one step back215 *216 * or217 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 218why, one step back218 * app.engine('html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 219–221why, one step back219 *220 * @param {String} ext221 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 222why, one step back222 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 223–225why, one step back223 * @api public224 */225Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 226why, one step back226app.engine = function(ext, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 227–286why, one step back227 if ('.' != ext[0]) ext = '.' + ext;228 this.engines[ext] = fn;229 return this;230};231232/**233 * Map the given param placeholder `name`(s) to the given callback(s).234 *235 * Param mapping is used to provide pre-conditions to routes236 * which us normalized placeholders. This callback has the same237 * signature as regular middleware, for example below when ":userId"238 * is used this function will be invoked in an attempt to load the user.239 *240 * app.param('userId', function(req, res, next, id){241 * User.find(id, function(err, user){242 * if (err) {243 * next(err);244 * } else if (user) {245 * req.user = user;246 * next();247 * } else {248 * next(new Error('failed to load user'));249 * }250 * });251 * });252 *253 * Passing a single function allows you to map logic254 * to the values passed to `app.param()`, for example255 * this is useful to provide coercion support in a concise manner.256 *257 * The following example maps regular expressions to param values258 * ensuring that they match, otherwise passing control to the next259 * route:260 *261 * app.param(function(name, regexp){262 * if (regexp instanceof RegExp) {263 * return function(req, res, next, val){264 * var captures;265 * if (captures = regexp.exec(String(val))) {266 * req.params[name] = captures;267 * next();268 * } else {269 * next('route');270 * }271 * }272 * }273 * });274 *275 * We can now use it as shown below, where "/commit/:commit" expects276 * that the value for ":commit" is at 5 or more digits. The capture277 * groups are then available as `req.params.commit` as we defined278 * in the function above.279 *280 * app.param('commit', /^\d{5,}$/);281 *282 * For more of this useful functionality take a look283 * at [express-params](http://github.com/visionmedia/express-params).284 *285 * @param {String|Array|Function} name286 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 287why, one step back287 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 288–303why, one step back288 * @api public289 */290291app.param = function(name, fn){292 var self = this293 , fns = [].slice.call(arguments, 1);294295 // array296 if (Array.isArray(name)) {297 name.forEach(function(name){298 fns.forEach(function(fn){299 self.param(name, fn);300 });301 });302 // param logic303 } else if ('function' == typeof name) {Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 304why, one step back304 this._router.param(name);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 305–308why, one step back305 // single306 } else {307 if (':' == name[0]) name = name.substr(1);308 fns.forEach(function(fn){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 309why, one step back309 self._router.param(name, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 310–320why, one step back310 });311 }312313 return this;314};315316/**317 * Register the given callback `fn` for the given `type`.318 *319 * @param {String} type320 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 321why, one step back321 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 322–381why, one step back322 * @api public323 */324325app.is = function(type, fn){326 if (!fn) return this.isCallbacks[type];327 this.isCallbacks[type] = fn;328 return this;329};330331/**332 * Assign `setting` to `val`, or return `setting`'s value.333 * Mounted servers inherit their parent server's settings.334 *335 * @param {String} setting336 * @param {String} val337 * @return {Server|Mixed} for chaining, or the setting value338 * @api public339 */340341app.set = function(setting, val){342 if (val === undefined) {343 if (this.settings.hasOwnProperty(setting)) {344 return this.settings[setting];345 } else if (this.parent) {346 return this.parent.set(setting);347 }348 } else {349 this.settings[setting] = val;350 return this;351 }352};353354/**355 * Check if `setting` is enabled.356 *357 * @param {String} setting358 * @return {Boolean}359 * @api public360 */361362app.enabled = function(setting){363 return !!this.set(setting);364};365366/**367 * Check if `setting` is disabled.368 *369 * @param {String} setting370 * @return {Boolean}371 * @api public372 */373374app.disabled = function(setting){375 return !this.set(setting);376};377378/**379 * Enable `setting`.380 *381 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 382why, one step back382 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 383–393why, one step back383 * @api public384 */385386app.enable = function(setting){387 return this.set(setting, true);388};389390/**391 * Disable `setting`.392 *393 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 394why, one step back394 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 395–406why, one step back395 * @api public396 */397398app.disable = function(setting){399 return this.set(setting, false);400};401402/**403 * Redirect `key` to `url`.404 *405 * @param {String} key406 * @param {String} urlAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 407why, one step back407 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 408–437why, one step back408 * @api public409 */410411app.redirect = function(key, url){412 this.redirects[key] = url;413 return this;414};415416/**417 * Configure callback for zero or more envs,418 * when no env is specified that callback will419 * be invoked for all environments. Any combination420 * can be used multiple times, in any order desired.421 *422 * Examples:423 *424 * app.configure(function(){425 * // executed for all envs426 * });427 *428 * app.configure('stage', function(){429 * // executed staging env430 * });431 *432 * app.configure('stage', 'production', function(){433 * // executed for stage and production434 * });435 *436 * @param {String} env...437 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 438why, one step back438 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 439–443why, one step back439 * @api public440 */441442app.configure = function(env, fn){443 var envs = 'all'Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 444why, one step back444 , args = [].slice.call(arguments);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 445–451why, one step back445 fn = args.pop();446 if (args.length) envs = args;447 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);448 return this;449};450451/**Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
lines 452–466why, one step back452 * Listen for connections.453 *454 * This method takes the same arguments455 * as node's `http.Server#listen()`. 456 *457 * @return {http.Server}458 * @api public459 */460461app.listen = function(){462 var server = http.createServer(this);463 return server.listen.apply(server, arguments);464};465466/**req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 467–471why, one step back467 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.468 */469470methods.forEach(function(method){471 app[method] = function(path){Added `app.get(setting)`. Closes #842Tj Holowaychukbefore adoptione0bd0d6
line 472why, one step back472 if ('get' == method && 1 == arguments.length) return this.set(path); Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 473why, one step back473 var args = [method].concat([].slice.call(arguments));fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 474why, one step back474 if (!this._usedRouter) this.use(this.router);Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 475why, one step back475 return this._router._route.apply(this._router, args);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 476–484why, one step back476 }477});478479/**480 * Special-cased "all" method, applying the given route `path`,481 * middleware, and callback to _every_ HTTP method.482 *483 * @param {String} path484 * @param {Function} ...Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 485why, one step back485 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 486–502why, one step back486 * @api public487 */488489app.all = function(path){490 var args = arguments;491 methods.forEach(function(method){492 if ('all' == method) return;493 app[method].apply(this, args);494 }, this);495 return this;496};497498// del -> delete alias499500app.del = app.delete;501502/**new ViewTj Holowaychukbefore adoptione1da383
line 503why, one step back503 * Render the given view `name` name with `options`req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 504–506why, one step back504 * and a callback accepting an error and the505 * rendered template string.506 *new ViewTj Holowaychukbefore adoptione1da383
line 507why, one step back507 * @param {String} namereq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 508–512why, one step back508 * @param {String|Function} options or fn509 * @param {Function} fn510 * @api public511 */512new ViewTj Holowaychukbefore adoptione1da383
line 513why, one step back513app.render = function(name, options, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 514–515why, one step back514 var self = this515 , opts = {}re-implemented "view cache"Tj Holowaychukbefore adoption8a38211
line 516why, one step back516 , cache = this.cachenew ViewTj Holowaychukbefore adoptione1da383
lines 517–518why, one step back517 , engines = this.engines518 , view;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 519–536why, one step back519520 // support callback function as second arg521 if ('function' == typeof options) {522 fn = options, options = {};523 }524525 // merge app.locals526 utils.merge(opts, this.locals);527528 // merge options.locals529 if (options.locals) {530 utils.merge(opts, options.locals);531 delete opts.locals;532 }533534 // merge options535 utils.merge(opts, options);536new ViewTj Holowaychukbefore adoptione1da383
lines 537–540why, one step back537 // set .cache unless explicitly provided538 opts.cache = null == opts.cache539 ? this.enabled('view cache')540 : opts.cache;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 541why, one step back541new ViewTj Holowaychukbefore adoptione1da383
lines 542–543why, one step back542 // primed cache543 if (opts.cache) view = cache[name];req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 544why, one step back544new ViewTj Holowaychukbefore adoptione1da383
lines 545–547why, one step back545 // view546 if (!view) {547 view = new View(name, {Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73
lines 548–549why, one step back548 defaultEngine: this.get('view engine')549 , root: this.get('views') || process.cwd() + '/views'new ViewTj Holowaychukbefore adoptione1da383
lines 550–551why, one step back550 , engines: engines551 });req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 552why, one step back552new ViewTj Holowaychukbefore adoptione1da383
lines 553–556why, one step back553 // prime the cache554 if (opts.cache) cache[name] = view;555 }556req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 557–558why, one step back557 // render558 try {new ViewTj Holowaychukbefore adoptione1da383
line 559why, one step back559 view.render(opts, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 560–563why, one step back560 } catch (err) {561 fn(err);562 }563};