application.js
This file
Path
lib/application.js
562 lines · 11.7 KiB · JavaScript
Last changed by
.parent testsTj Holowaychukbefore adoption5d593f2
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–108why, one step back106 };107108 // routerAdded `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
lines 109–110why, one step back109 this._router = new Router(this);110 this.routes = this._router.routes;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 111why, one step back111 this.__defineGetter__('router', function(){fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 112why, one step back112 this._usedRouter = true;Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 113why, one step back113 return self._router.middleware;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 114–133why, one step back114 });115116 // default locals117 this.locals.settings = this.settings;118119 // default production configuration120 this.configure('production', function(){121 this.enable('view cache');122 });123};124125/**126 * Remove routes matching the given `path`.127 *128 * @param {Route} path129 * @return {Boolean}130 * @api public131 */132133app.remove = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 134why, one step back134 return this._router.lookup('all', path).remove();req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 135–146why, one step back135};136137/**138 * Lookup routes defined with a path139 * equivalent to `path`.140 *141 * @param {Stirng} path142 * @return {Array}143 * @api public144 */145146app.lookup = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 147why, one step back147 return this._router.lookup('all', path);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 148–158why, one step back148};149150/**151 * Lookup routes matching the given `url`.152 *153 * @param {Stirng} url154 * @return {Array}155 * @api public156 */157158app.match = function(url){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 159why, one step back159 return this._router.match('all', url);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 160–162why, one step back160};161162/**docsTj Holowaychukbefore adoption2e22887
line 163why, one step back163 * Proxy `connect#use()` to apply settings toreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 164–166why, one step back164 * mounted applications.165 *166 * @param {String|Function|Server} routemisc refactoringTj Holowaychukbefore adoption1334a74
line 167why, one step back167 * @param {Function|Server} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 168why, one step back168 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 169–171why, one step back169 * @api public170 */171misc refactoringTj Holowaychukbefore adoption1334a74
line 172why, one step back172app.use = function(route, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 173–174why, one step back173 var app, home, handle;174misc refactoringTj Holowaychukbefore adoption1334a74
line 175why, one step back175 if ('string' != typeof route) fn = route, route = '/';req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 176–177why, one step back176177 // express appmisc refactoringTj Holowaychukbefore adoption1334a74
line 178why, one step back178 if (fn.handle && fn.set) app = fn;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 179–182why, one step back179180 // restore .app property on req and res181 if (app) {182 app.route = route;misc refactoringTj Holowaychukbefore adoption1334a74
line 183why, one step back183 fn = function(req, res, next) {req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 184–191why, one step back184 var orig = req.app;185 app.handle(req, res, function(err){186 req.app = res.app = orig;187 next(err);188 });189 };190 }191misc refactoringTj Holowaychukbefore adoption1334a74
line 192why, one step back192 connect.proto.use.call(this, route, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 193why, one step back193.parent testsTj Holowaychukbefore adoption5d593f2
line 194why, one step back194 // mounted an app, emit "mount"req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 195–196why, one step back195 // and adjust some settings196 if (app) {tweak res.redirect() mount route logicTj Holowaychukbefore adoption43d9f6e
lines 197–200why, one step back197 var root = this.get('root') || this.route;198 if ('/' == root) root = '';199 root = root + (app.get('root') || app.route);200 app.set('root', root);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 201why, one step back201 app.parent = this;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 202why, one step back202 app.emit('mount', this);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 203–212why, one step back203 }204205 return this;206};207208/**209 * Register the given template engine callback `fn`210 * as `ext`. For example we may wish to map ".html"211 * files to ejs rather than using the ".ejs" extension.212 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 213why, one step back213 * app.engine('.html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 214–216why, one step back214 *215 * or216 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 217why, one step back217 * app.engine('html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 218–220why, one step back218 *219 * @param {String} ext220 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 221why, one step back221 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 222–224why, one step back222 * @api public223 */224Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 225why, one step back225app.engine = function(ext, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 226–285why, one step back226 if ('.' != ext[0]) ext = '.' + ext;227 this.engines[ext] = fn;228 return this;229};230231/**232 * Map the given param placeholder `name`(s) to the given callback(s).233 *234 * Param mapping is used to provide pre-conditions to routes235 * which us normalized placeholders. This callback has the same236 * signature as regular middleware, for example below when ":userId"237 * is used this function will be invoked in an attempt to load the user.238 *239 * app.param('userId', function(req, res, next, id){240 * User.find(id, function(err, user){241 * if (err) {242 * next(err);243 * } else if (user) {244 * req.user = user;245 * next();246 * } else {247 * next(new Error('failed to load user'));248 * }249 * });250 * });251 *252 * Passing a single function allows you to map logic253 * to the values passed to `app.param()`, for example254 * this is useful to provide coercion support in a concise manner.255 *256 * The following example maps regular expressions to param values257 * ensuring that they match, otherwise passing control to the next258 * route:259 *260 * app.param(function(name, regexp){261 * if (regexp instanceof RegExp) {262 * return function(req, res, next, val){263 * var captures;264 * if (captures = regexp.exec(String(val))) {265 * req.params[name] = captures;266 * next();267 * } else {268 * next('route');269 * }270 * }271 * }272 * });273 *274 * We can now use it as shown below, where "/commit/:commit" expects275 * that the value for ":commit" is at 5 or more digits. The capture276 * groups are then available as `req.params.commit` as we defined277 * in the function above.278 *279 * app.param('commit', /^\d{5,}$/);280 *281 * For more of this useful functionality take a look282 * at [express-params](http://github.com/visionmedia/express-params).283 *284 * @param {String|Array|Function} name285 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 286why, one step back286 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 287–302why, one step back287 * @api public288 */289290app.param = function(name, fn){291 var self = this292 , fns = [].slice.call(arguments, 1);293294 // array295 if (Array.isArray(name)) {296 name.forEach(function(name){297 fns.forEach(function(fn){298 self.param(name, fn);299 });300 });301 // param logic302 } else if ('function' == typeof name) {Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 303why, one step back303 this._router.param(name);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 304–307why, one step back304 // single305 } else {306 if (':' == name[0]) name = name.substr(1);307 fns.forEach(function(fn){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 308why, one step back308 self._router.param(name, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 309–319why, one step back309 });310 }311312 return this;313};314315/**316 * Register the given callback `fn` for the given `type`.317 *318 * @param {String} type319 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 320why, one step back320 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 321–380why, one step back321 * @api public322 */323324app.is = function(type, fn){325 if (!fn) return this.isCallbacks[type];326 this.isCallbacks[type] = fn;327 return this;328};329330/**331 * Assign `setting` to `val`, or return `setting`'s value.332 * Mounted servers inherit their parent server's settings.333 *334 * @param {String} setting335 * @param {String} val336 * @return {Server|Mixed} for chaining, or the setting value337 * @api public338 */339340app.set = function(setting, val){341 if (val === undefined) {342 if (this.settings.hasOwnProperty(setting)) {343 return this.settings[setting];344 } else if (this.parent) {345 return this.parent.set(setting);346 }347 } else {348 this.settings[setting] = val;349 return this;350 }351};352353/**354 * Check if `setting` is enabled.355 *356 * @param {String} setting357 * @return {Boolean}358 * @api public359 */360361app.enabled = function(setting){362 return !!this.set(setting);363};364365/**366 * Check if `setting` is disabled.367 *368 * @param {String} setting369 * @return {Boolean}370 * @api public371 */372373app.disabled = function(setting){374 return !this.set(setting);375};376377/**378 * Enable `setting`.379 *380 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 381why, one step back381 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 382–392why, one step back382 * @api public383 */384385app.enable = function(setting){386 return this.set(setting, true);387};388389/**390 * Disable `setting`.391 *392 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 393why, one step back393 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 394–405why, one step back394 * @api public395 */396397app.disable = function(setting){398 return this.set(setting, false);399};400401/**402 * Redirect `key` to `url`.403 *404 * @param {String} key405 * @param {String} urlAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 406why, one step back406 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 407–436why, one step back407 * @api public408 */409410app.redirect = function(key, url){411 this.redirects[key] = url;412 return this;413};414415/**416 * Configure callback for zero or more envs,417 * when no env is specified that callback will418 * be invoked for all environments. Any combination419 * can be used multiple times, in any order desired.420 *421 * Examples:422 *423 * app.configure(function(){424 * // executed for all envs425 * });426 *427 * app.configure('stage', function(){428 * // executed staging env429 * });430 *431 * app.configure('stage', 'production', function(){432 * // executed for stage and production433 * });434 *435 * @param {String} env...436 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 437why, one step back437 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 438–442why, one step back438 * @api public439 */440441app.configure = function(env, fn){442 var envs = 'all'Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 443why, one step back443 , args = [].slice.call(arguments);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 444–450why, one step back444 fn = args.pop();445 if (args.length) envs = args;446 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);447 return this;448};449450/**Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
lines 451–465why, one step back451 * Listen for connections.452 *453 * This method takes the same arguments454 * as node's `http.Server#listen()`. 455 *456 * @return {http.Server}457 * @api public458 */459460app.listen = function(){461 var server = http.createServer(this);462 return server.listen.apply(server, arguments);463};464465/**req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 466–470why, one step back466 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.467 */468469methods.forEach(function(method){470 app[method] = function(path){Added `app.get(setting)`. Closes #842Tj Holowaychukbefore adoptione0bd0d6
line 471why, one step back471 if ('get' == method && 1 == arguments.length) return this.set(path); Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 472why, one step back472 var args = [method].concat([].slice.call(arguments));fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 473why, one step back473 if (!this._usedRouter) this.use(this.router);Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 474why, one step back474 return this._router._route.apply(this._router, args);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 475–483why, one step back475 }476});477478/**479 * Special-cased "all" method, applying the given route `path`,480 * middleware, and callback to _every_ HTTP method.481 *482 * @param {String} path483 * @param {Function} ...Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 484why, one step back484 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 485–501why, one step back485 * @api public486 */487488app.all = function(path){489 var args = arguments;490 methods.forEach(function(method){491 if ('all' == method) return;492 app[method].apply(this, args);493 }, this);494 return this;495};496497// del -> delete alias498499app.del = app.delete;500501/**new ViewTj Holowaychukbefore adoptione1da383
line 502why, one step back502 * Render the given view `name` name with `options`req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 503–505why, one step back503 * and a callback accepting an error and the504 * rendered template string.505 *new ViewTj Holowaychukbefore adoptione1da383
line 506why, one step back506 * @param {String} namereq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 507–511why, one step back507 * @param {String|Function} options or fn508 * @param {Function} fn509 * @api public510 */511new ViewTj Holowaychukbefore adoptione1da383
line 512why, one step back512app.render = function(name, options, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 513–514why, one step back513 var self = this514 , opts = {}re-implemented "view cache"Tj Holowaychukbefore adoption8a38211
line 515why, one step back515 , cache = this.cachenew ViewTj Holowaychukbefore adoptione1da383
lines 516–517why, one step back516 , engines = this.engines517 , view;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 518–535why, one step back518519 // support callback function as second arg520 if ('function' == typeof options) {521 fn = options, options = {};522 }523524 // merge app.locals525 utils.merge(opts, this.locals);526527 // merge options.locals528 if (options.locals) {529 utils.merge(opts, options.locals);530 delete opts.locals;531 }532533 // merge options534 utils.merge(opts, options);535new ViewTj Holowaychukbefore adoptione1da383
lines 536–539why, one step back536 // set .cache unless explicitly provided537 opts.cache = null == opts.cache538 ? this.enabled('view cache')539 : opts.cache;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 540why, one step back540new ViewTj Holowaychukbefore adoptione1da383
lines 541–542why, one step back541 // primed cache542 if (opts.cache) view = cache[name];req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 543why, one step back543new ViewTj Holowaychukbefore adoptione1da383
lines 544–546why, one step back544 // view545 if (!view) {546 view = new View(name, {Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73
lines 547–548why, one step back547 defaultEngine: this.get('view engine')548 , root: this.get('views') || process.cwd() + '/views'new ViewTj Holowaychukbefore adoptione1da383
lines 549–550why, one step back549 , engines: engines550 });req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 551why, one step back551new ViewTj Holowaychukbefore adoptione1da383
lines 552–555why, one step back552 // prime the cache553 if (opts.cache) cache[name] = view;554 }555req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 556–557why, one step back556 // render557 try {new ViewTj Holowaychukbefore adoptione1da383
line 558why, one step back558 view.render(opts, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 559–562why, one step back559 } catch (err) {560 fn(err);561 }562};