Skip to content
Myr

application.js

This file

Path

lib/application.js

590 lines · 12.3 KiB · JavaScript

Last changed by

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

History of this file

Take it

The walk stopped after 200 commits: lines older than e904132 point at it.

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

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')

new ViewTj Holowaychukbefore adoptione1da383

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 16–31why, one step back
16 , url = require('url')
17 , utils = connect.utils18 , path = require('path')19 , join = path.join20 , fs = require('fs')21 , qs = require('qs');2223/**24 * Expose `HTTPServer`.25 */2627var app = exports = module.exports = {};2829/**30 * Initialize the server.31 *

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 32–35why, one step back
32 * - setup default configuration
33 * - setup default middleware34 * - setup route reflection methods35 *

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 36–38why, one step back
36 * @api private
37 */38

refactoring boot logicTj Holowaychukbefore adoption8848004

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

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

app.lookup()Tj Holowaychukbefore adoption76b7d69

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

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

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

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

refactoring boot logicTj Holowaychukbefore adoption8848004

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 48why, one step back
48

miscTj Holowaychukbefore adoptiond4b08e9

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

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 50–51why, one step back
50 methods.forEach(function(method){
51 self.lookup[method] = function(path){

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

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

refactoring boot logicTj Holowaychukbefore adoption8848004

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

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

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

refactoring boot logicTj Holowaychukbefore adoption8848004

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

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

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

refactoring boot logicTj Holowaychukbefore adoption8848004

lines 61–78why, one step back
61 };
62 });6364 // del -> delete65 self.lookup.del = self.lookup.delete;66 self.match.del = self.match.delete;67 self.remove.del = self.remove.delete;68};6970/**71 * Initialize application configuration.72 *73 * @api private74 */7576app.defaultConfiguration = function(){77 var self = this;78

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

line 79why, one step back
79 // default settings

Fixed res.clearCookie(), default path to "root" settingTj Holowaychukbefore adoption0f2f3d4

line 80why, one step back
80 this.set('root', '/');

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 81why, one step back
81 this.set('env', process.env.NODE_ENV || 'development');

req.query is always presentTj Holowaychukbefore adoptiond1ce4c9

lines 82–83why, one step back
82
83 // always parse req.query

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 84–86why, one step back
84 this.use(connect.query());
8586 this.locals = function(obj){

misc refactoringTj Holowaychukbefore adoption4ed92d5

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 88why, one step back
88 return self;

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

lines 89–91why, one step back
89 };
9091 this.locals.use = function(fn){

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

lines 92–99why, one step back
92 if (3 == fn.length) {
93 self.viewCallbacks.push(fn);94 } else {95 self.viewCallbacks.push(function(req, res, done){96 fn(req, res);97 done();98 });99 }

app.locals.use(fn)Tj Holowaychukbefore adoption0f6ad43

line 100why, one step back
100 return this;

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 101–110why, one step back
101 };
102103 // expose objects to each other104 this.use(function(req, res, next){105 var charset;106 res.setHeader('X-Powered-By', 'Express');107 req.app = res.app = self;108 req.res = res;109 res.req = req;110 req.next = next;

no longer manipulate the req/res protos directlyTj Holowaychukbefore adoptiond249868

line 111why, one step back
111

make sure req/res protos are app-specificTj Holowaychukbefore adoptione27b224

lines 112–113why, one step back
112 req.__proto__ = self.request;
113 res.__proto__ = self.response;

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 114–115why, one step back
114
115 res.locals = function(obj){

misc refactoringTj Holowaychukbefore adoption4ed92d5

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 117–123why, one step back
117 return res;
118 };119120 next();121 });122123 // router

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

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

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

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

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

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

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 129–148why, one step back
129 });
130131 // default locals132 this.locals.settings = this.settings;133134 // default production configuration135 this.configure('production', function(){136 this.enable('view cache');137 });138};139140/**141 * Remove routes matching the given `path`.142 *143 * @param {Route} path144 * @return {Boolean}145 * @api public146 */147148app.remove = function(path){

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

line 149why, one step back
149 return this._router.lookup('all', path).remove();

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 150–161why, one step back
150};
151152/**153 * Lookup routes defined with a path154 * equivalent to `path`.155 *156 * @param {Stirng} path157 * @return {Array}158 * @api public159 */160161app.lookup = function(path){

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

line 162why, one step back
162 return this._router.lookup('all', path);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 163–173why, one step back
163};
164165/**166 * Lookup routes matching the given `url`.167 *168 * @param {Stirng} url169 * @return {Array}170 * @api public171 */172173app.match = function(url){

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

line 174why, one step back
174 return this._router.match('all', url);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 175–177why, one step back
175};
176177/**

docsTj Holowaychukbefore adoption2e22887

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 179–213why, one step back
179 * mounted applications.
180 *181 * @param {String|Function|Server} route182 * @param {Function|Server} middleware183 * @return {Server} for chaining184 * @api public185 */186187app.use = function(route, middleware){188 var app, home, handle;189190 if ('string' != typeof route) {191 middleware = route, route = '/';192 }193194 // express app195 if (middleware.handle && middleware.set) app = middleware;196197 // restore .app property on req and res198 if (app) {199 app.route = route;200 middleware = function(req, res, next) {201 var orig = req.app;202 app.handle(req, res, function(err){203 req.app = res.app = orig;204 next(err);205 });206 };207 }208209 connect.proto.use.call(this, route, middleware);210211 // mounted an app, invoke the hook212 // and adjust some settings213 if (app) {

tweak res.redirect() mount route logicTj Holowaychukbefore adoption43d9f6e

lines 214–217why, one step back
214 var root = this.get('root') || this.route;
215 if ('/' == root) root = '';216 root = root + (app.get('root') || app.route);217 app.set('root', root);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 218–255why, one step back
218 app.parent = this;
219 if (app.__mounted) app.__mounted.call(app, this);220 }221222 return this;223};224225/**226 * Assign a callback `fn` which is called227 * when this `Server` is passed to `Server#use()`.228 *229 * Examples:230 *231 * var app = express.createServer()232 * , blog = express.createServer();233 *234 * blog.mounted(function(parent){235 * // parent is app236 * // "this" is blog237 * });238 *239 * app.use(blog);240 *241 * @param {Function} fn242 * @return {Server} for chaining243 * @api public244 */245246app.mounted = function(fn){247 this.__mounted = fn;248 return this;249};250251/**252 * Register the given template engine callback `fn`253 * as `ext`. For example we may wish to map ".html"254 * files to ejs rather than using the ".ejs" extension.255 *

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 257–259why, one step back
257 *
258 * or259 *

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 261–267why, one step back
261 *
262 * @param {String} ext263 * @param {Function} fn264 * @return {Server} for chaining265 * @api public266 */267

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 269–345why, one step back
269 if ('.' != ext[0]) ext = '.' + ext;
270 this.engines[ext] = fn;271 return this;272};273274/**275 * Map the given param placeholder `name`(s) to the given callback(s).276 *277 * Param mapping is used to provide pre-conditions to routes278 * which us normalized placeholders. This callback has the same279 * signature as regular middleware, for example below when ":userId"280 * is used this function will be invoked in an attempt to load the user.281 *282 * app.param('userId', function(req, res, next, id){283 * User.find(id, function(err, user){284 * if (err) {285 * next(err);286 * } else if (user) {287 * req.user = user;288 * next();289 * } else {290 * next(new Error('failed to load user'));291 * }292 * });293 * });294 *295 * Passing a single function allows you to map logic296 * to the values passed to `app.param()`, for example297 * this is useful to provide coercion support in a concise manner.298 *299 * The following example maps regular expressions to param values300 * ensuring that they match, otherwise passing control to the next301 * route:302 *303 * app.param(function(name, regexp){304 * if (regexp instanceof RegExp) {305 * return function(req, res, next, val){306 * var captures;307 * if (captures = regexp.exec(String(val))) {308 * req.params[name] = captures;309 * next();310 * } else {311 * next('route');312 * }313 * }314 * }315 * });316 *317 * We can now use it as shown below, where "/commit/:commit" expects318 * that the value for ":commit" is at 5 or more digits. The capture319 * groups are then available as `req.params.commit` as we defined320 * in the function above.321 *322 * app.param('commit', /^\d{5,}$/);323 *324 * For more of this useful functionality take a look325 * at [express-params](http://github.com/visionmedia/express-params).326 *327 * @param {String|Array|Function} name328 * @param {Function} fn329 * @return {Server} for chaining330 * @api public331 */332333app.param = function(name, fn){334 var self = this335 , fns = [].slice.call(arguments, 1);336337 // array338 if (Array.isArray(name)) {339 name.forEach(function(name){340 fns.forEach(function(fn){341 self.param(name, fn);342 });343 });344 // param logic345 } else if ('function' == typeof name) {

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

line 346why, one step back
346 this._router.param(name);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 347–350why, one step back
347 // single
348 } else {349 if (':' == name[0]) name = name.substr(1);350 fns.forEach(function(fn){

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

line 351why, one step back
351 self._router.param(name, fn);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 352–485why, one step back
352 });
353 }354355 return this;356};357358/**359 * Register the given callback `fn` for the given `type`.360 *361 * @param {String} type362 * @param {Function} fn363 * @return {Server} for chaining364 * @api public365 */366367app.is = function(type, fn){368 if (!fn) return this.isCallbacks[type];369 this.isCallbacks[type] = fn;370 return this;371};372373/**374 * Assign `setting` to `val`, or return `setting`'s value.375 * Mounted servers inherit their parent server's settings.376 *377 * @param {String} setting378 * @param {String} val379 * @return {Server|Mixed} for chaining, or the setting value380 * @api public381 */382383app.set = function(setting, val){384 if (val === undefined) {385 if (this.settings.hasOwnProperty(setting)) {386 return this.settings[setting];387 } else if (this.parent) {388 return this.parent.set(setting);389 }390 } else {391 this.settings[setting] = val;392 return this;393 }394};395396/**397 * Check if `setting` is enabled.398 *399 * @param {String} setting400 * @return {Boolean}401 * @api public402 */403404app.enabled = function(setting){405 return !!this.set(setting);406};407408/**409 * Check if `setting` is disabled.410 *411 * @param {String} setting412 * @return {Boolean}413 * @api public414 */415416app.disabled = function(setting){417 return !this.set(setting);418};419420/**421 * Enable `setting`.422 *423 * @param {String} setting424 * @return {Server} for chaining425 * @api public426 */427428app.enable = function(setting){429 return this.set(setting, true);430};431432/**433 * Disable `setting`.434 *435 * @param {String} setting436 * @return {Server} for chaining437 * @api public438 */439440app.disable = function(setting){441 return this.set(setting, false);442};443444/**445 * Redirect `key` to `url`.446 *447 * @param {String} key448 * @param {String} url449 * @return {Server} for chaining450 * @api public451 */452453app.redirect = function(key, url){454 this.redirects[key] = url;455 return this;456};457458/**459 * Configure callback for zero or more envs,460 * when no env is specified that callback will461 * be invoked for all environments. Any combination462 * can be used multiple times, in any order desired.463 *464 * Examples:465 *466 * app.configure(function(){467 * // executed for all envs468 * });469 *470 * app.configure('stage', function(){471 * // executed staging env472 * });473 *474 * app.configure('stage', 'production', function(){475 * // executed for stage and production476 * });477 *478 * @param {String} env...479 * @param {Function} fn480 * @return {Server} for chaining481 * @api public482 */483484app.configure = function(env, fn){485 var envs = 'all'

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 487–498why, one step back
487 fn = args.pop();
488 if (args.length) envs = args;489 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);490 return this;491};492493/**494 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.495 */496497methods.forEach(function(method){498 app[method] = function(path){

Added `app.get(setting)`. Closes #842Tj Holowaychukbefore adoptione0bd0d6

line 499why, one step back
499 if ('get' == method && 1 == arguments.length) return this.set(path);

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

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

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

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

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

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 503–529why, one step back
503 }
504});505506/**507 * Special-cased "all" method, applying the given route `path`,508 * middleware, and callback to _every_ HTTP method.509 *510 * @param {String} path511 * @param {Function} ...512 * @return {Server} for chaining513 * @api public514 */515516app.all = function(path){517 var args = arguments;518 methods.forEach(function(method){519 if ('all' == method) return;520 app[method].apply(this, args);521 }, this);522 return this;523};524525// del -> delete alias526527app.del = app.delete;528529/**

new ViewTj Holowaychukbefore adoptione1da383

line 530why, one step back
530 * Render the given view `name` name with `options`

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 531–533why, one step back
531 * and a callback accepting an error and the
532 * rendered template string.533 *

new ViewTj Holowaychukbefore adoptione1da383

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

misc refactoringTj Holowaychukbefore adoption294bc8c

line 535why, one step back
535 * @param {String|Function} options or fn

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 536–539why, one step back
536 * @param {Function} fn
537 * @api public538 */539

new ViewTj Holowaychukbefore adoptione1da383

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 541why, one step back
541 var self = this

merging res.localsTj Holowaychukbefore adoption98e2c05

line 542why, one step back
542 , opts = {}

re-implemented "view cache"Tj Holowaychukbefore adoption8a38211

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

new ViewTj Holowaychukbefore adoptione1da383

lines 544–545why, one step back
544 , engines = this.engines
545 , view;

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 546–547why, one step back
546
547 // support callback function as second arg

misc refactoringTj Holowaychukbefore adoption294bc8c

lines 548–549why, one step back
548 if ('function' == typeof options) {
549 fn = options, options = {};

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 550–552why, one step back
550 }
551552 // merge app.locals

merging res.localsTj Holowaychukbefore adoption98e2c05

line 553why, one step back
553 utils.merge(opts, this.locals);

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 554why, one step back
554

merging res.localsTj Holowaychukbefore adoption98e2c05

lines 555–563why, one step back
555 // merge options.locals
556 if (options.locals) {557 utils.merge(opts, options.locals);558 delete opts.locals;559 }560561 // merge options562 utils.merge(opts, options);563

new ViewTj Holowaychukbefore adoptione1da383

lines 564–567why, one step back
564 // set .cache unless explicitly provided
565 opts.cache = null == opts.cache566 ? this.enabled('view cache')567 : opts.cache;

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 568why, one step back
568

new ViewTj Holowaychukbefore adoptione1da383

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 571why, one step back
571

new ViewTj Holowaychukbefore adoptione1da383

lines 572–574why, one step back
572 // view
573 if (!view) {574 view = new View(name, {

Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73

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

new ViewTj Holowaychukbefore adoptione1da383

lines 577–578why, one step back
577 , engines: engines
578 });

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

line 579why, one step back
579

new ViewTj Holowaychukbefore adoptione1da383

lines 580–583why, one step back
580 // prime the cache
581 if (opts.cache) cache[name] = view;582 }583

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 584–585why, one step back
584 // render
585 try {

new ViewTj Holowaychukbefore adoptione1da383

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

use app.engines instead of app.cacheTj Holowaychukbefore adoptione904132

lines 587–590why, one step back
587 } catch (err) {
588 fn(err);589 }590};