Skip to content
Myr

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 file

Take 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 back
1
2/*!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 back
15 , middleware = require('./middleware')

new ViewTj Holowaychukbefore adoptione1da383

line 16why, one step back
16 , View = require('./view')

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 17–19why, one step back
17 , 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 back
20 , http = require('http')

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 21–33why, one step back
21 , join = path.join
22 , 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 back
34 * - setup default configuration
35 * - setup default middleware36 * - setup route reflection methods37 *

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 38–40why, one step back
38 * @api private
39 */40

refactoring boot logicTj Holowaychukbefore adoption8848004

line 41why, one step back
41app.init = function(){

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 42why, one step back
42 var self = this;

app.lookup()Tj Holowaychukbefore adoption76b7d69

line 43why, one step back
43 this.cache = {};

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 44–47why, one step back
44 this.settings = {};
45 this.engines = {};46 this.redirects = {};47 this.isCallbacks = {};

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

line 48why, one step back
48 this.viewCallbacks = [];

refactoring boot logicTj Holowaychukbefore adoption8848004

line 49why, one step back
49 this.defaultConfiguration();

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 50why, one step back
50

miscTj Holowaychukbefore adoptiond4b08e9

line 51why, one step back
51 // route reflection methods

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 52–53why, one step back
52 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 back
54 return self._router.lookup(method, path);

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 55–57why, one step back
55 };
5657 self.match[method] = function(path){

Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc

line 58why, one step back
58 return self._router.match(method, path);

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 59–61why, one step back
59 };
6061 self.remove[method] = function(path){

Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc

line 62why, one step back
62 return self._router.lookup(method, path).remove();

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 63–80why, one step back
63 };
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;80

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 81–83why, one step back
81 // default settings
82 this.set('env', process.env.NODE_ENV || 'development');83

refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba

line 84why, one step back
84 // implicit middleware

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 85why, one step back
85 this.use(connect.query());

refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba

line 86why, one step back
86 this.use(middleware.init(this));

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 87why, one step back
87

docsTj Holowaychukbefore adoption6ae32fd

line 88why, one step back
88 // app locals

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 89why, one step back
89 this.locals = function(obj){

misc refactoringTj Holowaychukbefore adoption4ed92d5

line 90why, one step back
90 for (var key in obj) self.locals[key] = obj[key];

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 91why, one step back
91 return self;

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

lines 92–93why, one step back
92 };
93

docsTj Holowaychukbefore adoption6ae32fd

line 94why, one step back
94 // response locals

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

line 95why, one step back
95 this.locals.use = function(fn){

added sync signature for app.locals.use()Tj Holowaychukbefore adoption965e1d6

lines 96–103why, one step back
96 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 back
104 return this;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 105–107why, one step back
105 };
106107 // router

Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc

lines 108–109why, one step back
108 this._router = new Router(this);
109 this.routes = this._router.routes;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 110why, one step back
110 this.__defineGetter__('router', function(){

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

line 111why, one step back
111 this._usedRouter = true;

Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc

line 112why, one step back
112 return self._router.middleware;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 113–132why, one step back
113 });
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 back
133 return this._router.lookup('all', path).remove();

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 134–145why, one step back
134};
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 back
146 return this._router.lookup('all', path);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 147–157why, one step back
147};
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 back
158 return this._router.match('all', url);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 159–161why, one step back
159};
160161/**

docsTj Holowaychukbefore adoption2e22887

line 162why, one step back
162 * Proxy `connect#use()` to apply settings to

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 163–165why, one step back
163 * mounted applications.
164 *165 * @param {String|Function|Server} route

misc refactoringTj Holowaychukbefore adoption1334a74

line 166why, one step back
166 * @param {Function|Server} fn

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 167why, one step back
167 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 168–170why, one step back
168 * @api public
169 */170

misc refactoringTj Holowaychukbefore adoption1334a74

line 171why, one step back
171app.use = function(route, fn){

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 172–173why, one step back
172 var app, home, handle;
173

misc refactoringTj Holowaychukbefore adoption1334a74

line 174why, one step back
174 if ('string' != typeof route) fn = route, route = '/';

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 175–176why, one step back
175
176 // express app

misc refactoringTj Holowaychukbefore adoption1334a74

line 177why, one step back
177 if (fn.handle && fn.set) app = fn;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 178–181why, one step back
178
179 // restore .app property on req and res180 if (app) {181 app.route = route;

misc refactoringTj Holowaychukbefore adoption1334a74

line 182why, one step back
182 fn = function(req, res, next) {

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 183–190why, one step back
183 var orig = req.app;
184 app.handle(req, res, function(err){185 req.app = res.app = orig;186 next(err);187 });188 };189 }190

misc refactoringTj Holowaychukbefore adoption1334a74

line 191why, one step back
191 connect.proto.use.call(this, route, fn);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 192why, one step back
192

removed "root" settingTj Holowaychukbefore adoption87b991c

line 193why, one step back
193 // mounted an app

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 194–195why, one step back
194 if (app) {
195 app.parent = this;

Added: emit "mount" event on the app being mountedTj Holowaychukbefore adoptionb6611c8

line 196why, one step back
196 app.emit('mount', this);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 197–206why, one step back
197 }
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 back
207 * app.engine('.html', require('ejs').render);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 208–210why, one step back
208 *
209 * or210 *

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

line 211why, one step back
211 * app.engine('html', require('ejs').render);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 212–214why, one step back
212 *
213 * @param {String} ext214 * @param {Function} fn

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 215why, one step back
215 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 216–218why, one step back
216 * @api public
217 */218

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

line 219why, one step back
219app.engine = function(ext, fn){

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 220–279why, one step back
220 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} fn

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 280why, one step back
280 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 281–296why, one step back
281 * @api public
282 */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 back
297 this._router.param(name);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 298–301why, one step back
298 // single
299 } 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 back
302 self._router.param(name, fn);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 303–313why, one step back
303 });
304 }305306 return this;307};308309/**310 * Register the given callback `fn` for the given `type`.311 *312 * @param {String} type313 * @param {Function} fn

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 314why, one step back
314 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 315–374why, one step back
315 * @api public
316 */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} setting

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 375why, one step back
375 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 376–386why, one step back
376 * @api public
377 */378379app.enable = function(setting){380 return this.set(setting, true);381};382383/**384 * Disable `setting`.385 *386 * @param {String} setting

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 387why, one step back
387 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 388–399why, one step back
388 * @api public
389 */390391app.disable = function(setting){392 return this.set(setting, false);393};394395/**396 * Redirect `key` to `url`.397 *398 * @param {String} key399 * @param {String} url

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 400why, one step back
400 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 401–430why, one step back
401 * @api public
402 */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} fn

Added app.listen() as a shortcut for http.createServer(app).listen()Tj Holowaychukbefore adoption9082e74

line 431why, one step back
431 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 432–436why, one step back
432 * @api public
433 */434435app.configure = function(env, fn){436 var envs = 'all'

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

line 437why, one step back
437 , args = [].slice.call(arguments);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 438–444why, one step back
438 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 back
445 * 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 back
460 * 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 back
465 if ('get' == method && 1 == arguments.length) return this.set(path);

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

line 466why, one step back
466 var args = [method].concat([].slice.call(arguments));

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

line 467why, one step back
467 if (!this._usedRouter) this.use(this.router);

Added `app.routes`, replacing `app.routes.all()`. Closes #811Tj Holowaychukbefore adoption62257bc

line 468why, one step back
468 return this._router._route.apply(this._router, args);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 469–477why, one step back
469 }
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 back
478 * @return {app} for chaining

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 479–495why, one step back
479 * @api public
480 */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 back
496 * Render the given view `name` name with `options`

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 497–499why, one step back
497 * and a callback accepting an error and the
498 * rendered template string.499 *

new ViewTj Holowaychukbefore adoptione1da383

line 500why, one step back
500 * @param {String} name

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 501–505why, one step back
501 * @param {String|Function} options or fn
502 * @param {Function} fn503 * @api public504 */505

new ViewTj Holowaychukbefore adoptione1da383

line 506why, one step back
506app.render = function(name, options, fn){

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 507–508why, one step back
507 var self = this
508 , opts = {}

re-implemented "view cache"Tj Holowaychukbefore adoption8a38211

line 509why, one step back
509 , cache = this.cache

new ViewTj Holowaychukbefore adoptione1da383

lines 510–511why, one step back
510 , engines = this.engines
511 , view;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 512–529why, one step back
512
513 // 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);529

new ViewTj Holowaychukbefore adoptione1da383

lines 530–533why, one step back
530 // set .cache unless explicitly provided
531 opts.cache = null == opts.cache532 ? this.enabled('view cache')533 : opts.cache;

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 534why, one step back
534

new ViewTj Holowaychukbefore adoptione1da383

lines 535–536why, one step back
535 // primed cache
536 if (opts.cache) view = cache[name];

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 537why, one step back
537

new ViewTj Holowaychukbefore adoptione1da383

lines 538–540why, one step back
538 // view
539 if (!view) {540 view = new View(name, {

Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73

lines 541–542why, one step back
541 defaultEngine: this.get('view engine')
542 , root: this.get('views') || process.cwd() + '/views'

new ViewTj Holowaychukbefore adoptione1da383

lines 543–544why, one step back
543 , engines: engines
544 });

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 545why, one step back
545

new ViewTj Holowaychukbefore adoptione1da383

lines 546–549why, one step back
546 // prime the cache
547 if (opts.cache) cache[name] = view;548 }549

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 550–551why, one step back
550 // render
551 try {

new ViewTj Holowaychukbefore adoptione1da383

line 552why, one step back
552 view.render(opts, fn);

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 553–556why, one step back
553 } catch (err) {
554 fn(err);555 }556};