application.js
This file
Path
lib/application.js
556 lines · 11.5 KiB · JavaScript
Last changed by
removed "root" settingTj Holowaychukbefore adoption87b991c
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
lines 81–83why, one step back81 // default settings82 this.set('env', process.env.NODE_ENV || 'development');83refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba
line 84why, one step back84 // implicit middlewarereq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 85why, one step back85 this.use(connect.query());refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba
line 86why, one step back86 this.use(middleware.init(this));req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 87why, one step back87docsTj Holowaychukbefore adoption6ae32fd
line 88why, one step back88 // app localsreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 89why, one step back89 this.locals = function(obj){misc refactoringTj Holowaychukbefore adoption4ed92d5
line 90why, one step back90 for (var key in obj) self.locals[key] = obj[key];req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 91why, one step back91 return self;app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
lines 92–93why, one step back92 };93docsTj Holowaychukbefore adoption6ae32fd
line 94why, one step back94 // response localsapp.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
line 95why, one step back95 this.locals.use = function(fn){added sync signature for app.locals.use()Tj Holowaychukbefore adoption965e1d6
lines 96–103why, one step back96 if (3 == fn.length) {97 self.viewCallbacks.push(fn);98 } else {99 self.viewCallbacks.push(function(req, res, done){100 fn(req, res);101 done();102 });103 }app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43
line 104why, one step back104 return this;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 105–107why, one step back105 };106107 // routerAdded `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
lines 108–109why, one step back108 this._router = new Router(this);109 this.routes = this._router.routes;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 110why, one step back110 this.__defineGetter__('router', function(){fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 111why, one step back111 this._usedRouter = true;Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 112why, one step back112 return self._router.middleware;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 113–132why, one step back113 });114115 // default locals116 this.locals.settings = this.settings;117118 // default production configuration119 this.configure('production', function(){120 this.enable('view cache');121 });122};123124/**125 * Remove routes matching the given `path`.126 *127 * @param {Route} path128 * @return {Boolean}129 * @api public130 */131132app.remove = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 133why, one step back133 return this._router.lookup('all', path).remove();req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 134–145why, one step back134};135136/**137 * Lookup routes defined with a path138 * equivalent to `path`.139 *140 * @param {Stirng} path141 * @return {Array}142 * @api public143 */144145app.lookup = function(path){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 146why, one step back146 return this._router.lookup('all', path);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 147–157why, one step back147};148149/**150 * Lookup routes matching the given `url`.151 *152 * @param {Stirng} url153 * @return {Array}154 * @api public155 */156157app.match = function(url){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 158why, one step back158 return this._router.match('all', url);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 159–161why, one step back159};160161/**docsTj Holowaychukbefore adoption2e22887
line 162why, one step back162 * Proxy `connect#use()` to apply settings toreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 163–165why, one step back163 * mounted applications.164 *165 * @param {String|Function|Server} routemisc refactoringTj Holowaychukbefore adoption1334a74
line 166why, one step back166 * @param {Function|Server} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 167why, one step back167 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 168–170why, one step back168 * @api public169 */170misc refactoringTj Holowaychukbefore adoption1334a74
line 171why, one step back171app.use = function(route, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 172–173why, one step back172 var app, home, handle;173misc refactoringTj Holowaychukbefore adoption1334a74
line 174why, one step back174 if ('string' != typeof route) fn = route, route = '/';req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 175–176why, one step back175176 // express appmisc refactoringTj Holowaychukbefore adoption1334a74
line 177why, one step back177 if (fn.handle && fn.set) app = fn;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 178–181why, one step back178179 // restore .app property on req and res180 if (app) {181 app.route = route;misc refactoringTj Holowaychukbefore adoption1334a74
line 182why, one step back182 fn = function(req, res, next) {req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 183–190why, one step back183 var orig = req.app;184 app.handle(req, res, function(err){185 req.app = res.app = orig;186 next(err);187 });188 };189 }190misc refactoringTj Holowaychukbefore adoption1334a74
line 191why, one step back191 connect.proto.use.call(this, route, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 192why, one step back192removed "root" settingTj Holowaychukbefore adoption87b991c
line 193why, one step back193 // mounted an appreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 194–195why, one step back194 if (app) {195 app.parent = this;Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8
line 196why, one step back196 app.emit('mount', this);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 197–206why, one step back197 }198199 return this;200};201202/**203 * Register the given template engine callback `fn`204 * as `ext`. For example we may wish to map ".html"205 * files to ejs rather than using the ".ejs" extension.206 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 207why, one step back207 * app.engine('.html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 208–210why, one step back208 *209 * or210 *Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 211why, one step back211 * app.engine('html', require('ejs').render);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 212–214why, one step back212 *213 * @param {String} ext214 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 215why, one step back215 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 216–218why, one step back216 * @api public217 */218Added app.engine() testsTj Holowaychukbefore adoption66f8ca5
line 219why, one step back219app.engine = function(ext, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 220–279why, one step back220 if ('.' != ext[0]) ext = '.' + ext;221 this.engines[ext] = fn;222 return this;223};224225/**226 * Map the given param placeholder `name`(s) to the given callback(s).227 *228 * Param mapping is used to provide pre-conditions to routes229 * which us normalized placeholders. This callback has the same230 * signature as regular middleware, for example below when ":userId"231 * is used this function will be invoked in an attempt to load the user.232 *233 * app.param('userId', function(req, res, next, id){234 * User.find(id, function(err, user){235 * if (err) {236 * next(err);237 * } else if (user) {238 * req.user = user;239 * next();240 * } else {241 * next(new Error('failed to load user'));242 * }243 * });244 * });245 *246 * Passing a single function allows you to map logic247 * to the values passed to `app.param()`, for example248 * this is useful to provide coercion support in a concise manner.249 *250 * The following example maps regular expressions to param values251 * ensuring that they match, otherwise passing control to the next252 * route:253 *254 * app.param(function(name, regexp){255 * if (regexp instanceof RegExp) {256 * return function(req, res, next, val){257 * var captures;258 * if (captures = regexp.exec(String(val))) {259 * req.params[name] = captures;260 * next();261 * } else {262 * next('route');263 * }264 * }265 * }266 * });267 *268 * We can now use it as shown below, where "/commit/:commit" expects269 * that the value for ":commit" is at 5 or more digits. The capture270 * groups are then available as `req.params.commit` as we defined271 * in the function above.272 *273 * app.param('commit', /^\d{5,}$/);274 *275 * For more of this useful functionality take a look276 * at [express-params](http://github.com/visionmedia/express-params).277 *278 * @param {String|Array|Function} name279 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 280why, one step back280 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 281–296why, one step back281 * @api public282 */283284app.param = function(name, fn){285 var self = this286 , fns = [].slice.call(arguments, 1);287288 // array289 if (Array.isArray(name)) {290 name.forEach(function(name){291 fns.forEach(function(fn){292 self.param(name, fn);293 });294 });295 // param logic296 } else if ('function' == typeof name) {Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 297why, one step back297 this._router.param(name);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 298–301why, one step back298 // single299 } else {300 if (':' == name[0]) name = name.substr(1);301 fns.forEach(function(fn){Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 302why, one step back302 self._router.param(name, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 303–313why, one step back303 });304 }305306 return this;307};308309/**310 * Register the given callback `fn` for the given `type`.311 *312 * @param {String} type313 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 314why, one step back314 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 315–374why, one step back315 * @api public316 */317318app.is = function(type, fn){319 if (!fn) return this.isCallbacks[type];320 this.isCallbacks[type] = fn;321 return this;322};323324/**325 * Assign `setting` to `val`, or return `setting`'s value.326 * Mounted servers inherit their parent server's settings.327 *328 * @param {String} setting329 * @param {String} val330 * @return {Server|Mixed} for chaining, or the setting value331 * @api public332 */333334app.set = function(setting, val){335 if (val === undefined) {336 if (this.settings.hasOwnProperty(setting)) {337 return this.settings[setting];338 } else if (this.parent) {339 return this.parent.set(setting);340 }341 } else {342 this.settings[setting] = val;343 return this;344 }345};346347/**348 * Check if `setting` is enabled.349 *350 * @param {String} setting351 * @return {Boolean}352 * @api public353 */354355app.enabled = function(setting){356 return !!this.set(setting);357};358359/**360 * Check if `setting` is disabled.361 *362 * @param {String} setting363 * @return {Boolean}364 * @api public365 */366367app.disabled = function(setting){368 return !this.set(setting);369};370371/**372 * Enable `setting`.373 *374 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 375why, one step back375 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 376–386why, one step back376 * @api public377 */378379app.enable = function(setting){380 return this.set(setting, true);381};382383/**384 * Disable `setting`.385 *386 * @param {String} settingAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 387why, one step back387 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 388–399why, one step back388 * @api public389 */390391app.disable = function(setting){392 return this.set(setting, false);393};394395/**396 * Redirect `key` to `url`.397 *398 * @param {String} key399 * @param {String} urlAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 400why, one step back400 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 401–430why, one step back401 * @api public402 */403404app.redirect = function(key, url){405 this.redirects[key] = url;406 return this;407};408409/**410 * Configure callback for zero or more envs,411 * when no env is specified that callback will412 * be invoked for all environments. Any combination413 * can be used multiple times, in any order desired.414 *415 * Examples:416 *417 * app.configure(function(){418 * // executed for all envs419 * });420 *421 * app.configure('stage', function(){422 * // executed staging env423 * });424 *425 * app.configure('stage', 'production', function(){426 * // executed for stage and production427 * });428 *429 * @param {String} env...430 * @param {Function} fnAdded app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 431why, one step back431 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 432–436why, one step back432 * @api public433 */434435app.configure = function(env, fn){436 var envs = 'all'Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 437why, one step back437 , args = [].slice.call(arguments);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 438–444why, one step back438 fn = args.pop();439 if (args.length) envs = args;440 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);441 return this;442};443444/**Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
lines 445–459why, one step back445 * Listen for connections.446 *447 * This method takes the same arguments448 * as node's `http.Server#listen()`. 449 *450 * @return {http.Server}451 * @api public452 */453454app.listen = function(){455 var server = http.createServer(this);456 return server.listen.apply(server, arguments);457};458459/**req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 460–464why, one step back460 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.461 */462463methods.forEach(function(method){464 app[method] = function(path){Added `app.get(setting)`. Closes #842Tj Holowaychukbefore adoptione0bd0d6
line 465why, one step back465 if ('get' == method && 1 == arguments.length) return this.set(path); Removed toArray() utilTj Holowaychukbefore adoption7a7d77e
line 466why, one step back466 var args = [method].concat([].slice.call(arguments));fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284
line 467why, one step back467 if (!this._usedRouter) this.use(this.router);Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc
line 468why, one step back468 return this._router._route.apply(this._router, args);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 469–477why, one step back469 }470});471472/**473 * Special-cased "all" method, applying the given route `path`,474 * middleware, and callback to _every_ HTTP method.475 *476 * @param {String} path477 * @param {Function} ...Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74
line 478why, one step back478 * @return {app} for chainingreq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 479–495why, one step back479 * @api public480 */481482app.all = function(path){483 var args = arguments;484 methods.forEach(function(method){485 if ('all' == method) return;486 app[method].apply(this, args);487 }, this);488 return this;489};490491// del -> delete alias492493app.del = app.delete;494495/**new ViewTj Holowaychukbefore adoptione1da383
line 496why, one step back496 * Render the given view `name` name with `options`req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 497–499why, one step back497 * and a callback accepting an error and the498 * rendered template string.499 *new ViewTj Holowaychukbefore adoptione1da383
line 500why, one step back500 * @param {String} namereq.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 501–505why, one step back501 * @param {String|Function} options or fn502 * @param {Function} fn503 * @api public504 */505new ViewTj Holowaychukbefore adoptione1da383
line 506why, one step back506app.render = function(name, options, fn){req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 507–508why, one step back507 var self = this508 , opts = {}re-implemented "view cache"Tj Holowaychukbefore adoption8a38211
line 509why, one step back509 , cache = this.cachenew ViewTj Holowaychukbefore adoptione1da383
lines 510–511why, one step back510 , engines = this.engines511 , view;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 512–529why, one step back512513 // support callback function as second arg514 if ('function' == typeof options) {515 fn = options, options = {};516 }517518 // merge app.locals519 utils.merge(opts, this.locals);520521 // merge options.locals522 if (options.locals) {523 utils.merge(opts, options.locals);524 delete opts.locals;525 }526527 // merge options528 utils.merge(opts, options);529new ViewTj Holowaychukbefore adoptione1da383
lines 530–533why, one step back530 // set .cache unless explicitly provided531 opts.cache = null == opts.cache532 ? this.enabled('view cache')533 : opts.cache;req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 534why, one step back534new ViewTj Holowaychukbefore adoptione1da383
lines 535–536why, one step back535 // primed cache536 if (opts.cache) view = cache[name];req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 537why, one step back537new ViewTj Holowaychukbefore adoptione1da383
lines 538–540why, one step back538 // view539 if (!view) {540 view = new View(name, {Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73
lines 541–542why, one step back541 defaultEngine: this.get('view engine')542 , root: this.get('views') || process.cwd() + '/views'new ViewTj Holowaychukbefore adoptione1da383
lines 543–544why, one step back543 , engines: engines544 });req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
line 545why, one step back545new ViewTj Holowaychukbefore adoptione1da383
lines 546–549why, one step back546 // prime the cache547 if (opts.cache) cache[name] = view;548 }549req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 550–551why, one step back550 // render551 try {new ViewTj Holowaychukbefore adoptione1da383
line 552why, one step back552 view.render(opts, fn);req.query is always presentTj Holowaychukbefore adoptiond1ce4c9
lines 553–556why, one step back553 } catch (err) {554 fn(err);555 }556};