Skip to content
Myr

application.js

This file

Path

lib/application.js

529 lines · 11.2 KiB · JavaScript

Last changed by

ocdTJ Holowaychukbefore adoptiond115798

History of this file

Take it

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

ocdTJ Holowaychukbefore adoptiond115798

line 1why, one step back
1

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 2–14why, one step back
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')

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3360613

line 16why, one step back
16 , debug = require('debug')('express:application')

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 17–20why, one step back
17 , View = require('./view')
18 , url = require('url')19 , utils = connect.utils20 , path = require('path')

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

line 21why, one step back
21 , http = require('http')

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

line 22why, one step back
22 , join = path.join

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3360613

line 23why, one step back
23 , fs = require('fs');

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 24–25why, one step back
24
25/**

typoTj Holowaychukbefore adoptionc30c2f8

line 26why, one step back
26 * Application prototype.

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 27–75why, one step back
27 */
2829var app = exports = module.exports = {};3031/**32 * Initialize the server.33 *34 * - setup default configuration35 * - setup default middleware36 * - setup route reflection methods37 *38 * @api private39 */4041app.init = function(){42 var self = this;43 this.cache = {};44 this.settings = {};45 this.engines = {};46 this.viewCallbacks = [];47 this.defaultConfiguration();4849 // route reflection methods50 methods.forEach(function(method){51 self.lookup[method] = function(path){52 return self._router.lookup(method, path);53 };5455 self.remove[method] = function(path){56 return self._router.lookup(method, path).remove();57 };58 });5960 // del -> delete61 self.lookup.del = self.lookup.delete;62 self.remove.del = self.remove.delete;63};6465/**66 * Initialize application configuration.67 *68 * @api private69 */7071app.defaultConfiguration = function(){72 var self = this;7374 // default settings75 this.set('env', process.env.NODE_ENV || 'development');

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3360613

line 76why, one step back
76 debug('booting in %s mode', this.get('env'));

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

line 77why, one step back
77

refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

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

refactored init middleware into its own fileTj Holowaychukbefore adoption4c316ba

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

line 81why, one step back
81

docsTj Holowaychukbefore adoption6ae32fd

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 83–87why, one step back
83 this.locals = function(obj){
84 for (var key in obj) self.locals[key] = obj[key];85 return self;86 };87

docsTj Holowaychukbefore adoption6ae32fd

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 89–104why, one step back
89 this.locals.use = function(fn){
90 if (3 == fn.length) {91 self.viewCallbacks.push(fn);92 } else {93 self.viewCallbacks.push(function(req, res, done){94 fn(req, res);95 done();96 });97 }98 return this;99 };100101 // router102 this._router = new Router(this);103 this.routes = this._router.routes;104 this.__defineGetter__('router', function(){

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

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

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

lines 106–108why, one step back
106 this._router.caseSensitive = this.enabled('case sensitive routing');
107 this._router.strict = this.enabled('strict routing');108 return this._router.middleware;

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 109–113why, one step back
109 });
110111 // default locals112 this.locals.settings = this.settings;113

Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa

lines 114–118why, one step back
114 // default configuration
115 this.configure('development', function(){116 this.set('json spaces', 2);117 });118

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 119–139why, one step back
119 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){133 return this._router.lookup('all', path).remove();134};135136/**137 * Lookup routes defined with a path138 * equivalent to `path`.139 *

Merge pull request #968 from peters/patch-1TJ Holowaychukbefore adoption4eb8bc0

line 140why, one step back
140 * @param {String} path

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 141–149why, one step back
141 * @return {Array}
142 * @api public143 */144145app.lookup = function(path){146 return this._router.lookup('all', path);147};148149/**

docsTj Holowaychukbefore adoption2e22887

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 151–153why, one step back
151 * mounted applications.
152 *153 * @param {String|Function|Server} route

misc refactoringTj Holowaychukbefore adoption1334a74

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

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 156–158why, one step back
156 * @api public
157 */158

misc refactoringTj Holowaychukbefore adoption1334a74

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 160–161why, one step back
160 var app, home, handle;
161

app.path()Tj Holowaychukbefore adoption17a831e

line 162why, one step back
162 // default route to '/'

misc refactoringTj Holowaychukbefore adoption1334a74

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 164–165why, one step back
164
165 // express app

misc refactoringTj Holowaychukbefore adoption1334a74

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 167–170why, one step back
167
168 // restore .app property on req and res169 if (app) {170 app.route = route;

misc refactoringTj Holowaychukbefore adoption1334a74

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 172–179why, one step back
172 var orig = req.app;
173 app.handle(req, res, function(err){174 req.app = res.app = orig;175 next(err);176 });177 };178 }179

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3360613

line 180why, one step back
180 debug('use %s %s', route, fn.name || 'unnamed');

misc refactoringTj Holowaychukbefore adoption1334a74

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

line 182why, one step back
182

removed "root" settingTj Holowaychukbefore adoption87b991c

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

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

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 187–196why, one step back
187 }
188189 return this;190};191192/**193 * Register the given template engine callback `fn`194 * as `ext`. For example we may wish to map ".html"195 * files to ejs rather than using the ".ejs" extension.196 *

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 198–200why, one step back
198 *
199 * or200 *

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 202–204why, one step back
202 *
203 * @param {String} ext204 * @param {Function} fn

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 206–208why, one step back
206 * @api public
207 */208

Added app.engine() testsTj Holowaychukbefore adoption66f8ca5

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 210–269why, one step back
210 if ('.' != ext[0]) ext = '.' + ext;
211 this.engines[ext] = fn;212 return this;213};214215/**216 * Map the given param placeholder `name`(s) to the given callback(s).217 *218 * Param mapping is used to provide pre-conditions to routes219 * which us normalized placeholders. This callback has the same220 * signature as regular middleware, for example below when ":userId"221 * is used this function will be invoked in an attempt to load the user.222 *223 * app.param('userId', function(req, res, next, id){224 * User.find(id, function(err, user){225 * if (err) {226 * next(err);227 * } else if (user) {228 * req.user = user;229 * next();230 * } else {231 * next(new Error('failed to load user'));232 * }233 * });234 * });235 *236 * Passing a single function allows you to map logic237 * to the values passed to `app.param()`, for example238 * this is useful to provide coercion support in a concise manner.239 *240 * The following example maps regular expressions to param values241 * ensuring that they match, otherwise passing control to the next242 * route:243 *244 * app.param(function(name, regexp){245 * if (regexp instanceof RegExp) {246 * return function(req, res, next, val){247 * var captures;248 * if (captures = regexp.exec(String(val))) {249 * req.params[name] = captures;250 * next();251 * } else {252 * next('route');253 * }254 * }255 * }256 * });257 *258 * We can now use it as shown below, where "/commit/:commit" expects259 * that the value for ":commit" is at 5 or more digits. The capture260 * groups are then available as `req.params.commit` as we defined261 * in the function above.262 *263 * app.param('commit', /^\d{5,}$/);264 *265 * For more of this useful functionality take a look266 * at [express-params](http://github.com/visionmedia/express-params).267 *268 * @param {String|Array|Function} name269 * @param {Function} fn

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 271–309why, one step back
271 * @api public
272 */273274app.param = function(name, fn){275 var self = this276 , fns = [].slice.call(arguments, 1);277278 // array279 if (Array.isArray(name)) {280 name.forEach(function(name){281 fns.forEach(function(fn){282 self.param(name, fn);283 });284 });285 // param logic286 } else if ('function' == typeof name) {287 this._router.param(name);288 // single289 } else {290 if (':' == name[0]) name = name.substr(1);291 fns.forEach(function(fn){292 self._router.param(name, fn);293 });294 }295296 return this;297};298299/**300 * Assign `setting` to `val`, or return `setting`'s value.301 * Mounted servers inherit their parent server's settings.302 *303 * @param {String} setting304 * @param {String} val305 * @return {Server|Mixed} for chaining, or the setting value306 * @api public307 */308309app.set = function(setting, val){

Fixed app.set() with undefinedTj Holowaychukbefore adoption7d24c2b

line 310why, one step back
310 if (1 == arguments.length) {

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 311–319why, one step back
311 if (this.settings.hasOwnProperty(setting)) {
312 return this.settings[setting];313 } else if (this.parent) {314 return this.parent.set(setting);315 }316 } else {317 this.settings[setting] = val;318 return this;319 }

app.path()Tj Holowaychukbefore adoption17a831e

lines 320–334why, one step back
320};
321322/**323 * Return the app's absolute pathname324 * based on the parent(s) that have325 * mounted it.326 *327 * @return {String}328 * @api private329 */330331app.path = function(){332 return this.parent333 ? this.parent.path() + this.route334 : '';

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 335–364why, one step back
335};
336337/**338 * Check if `setting` is enabled.339 *340 * @param {String} setting341 * @return {Boolean}342 * @api public343 */344345app.enabled = function(setting){346 return !!this.set(setting);347};348349/**350 * Check if `setting` is disabled.351 *352 * @param {String} setting353 * @return {Boolean}354 * @api public355 */356357app.disabled = function(setting){358 return !this.set(setting);359};360361/**362 * Enable `setting`.363 *364 * @param {String} setting

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 366–376why, one step back
366 * @api public
367 */368369app.enable = function(setting){370 return this.set(setting, true);371};372373/**374 * Disable `setting`.375 *376 * @param {String} setting

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 378–406why, one step back
378 * @api public
379 */380381app.disable = function(setting){382 return this.set(setting, false);383};384385/**386 * Configure callback for zero or more envs,387 * when no env is specified that callback will388 * be invoked for all environments. Any combination389 * can be used multiple times, in any order desired.390 *391 * Examples:392 *393 * app.configure(function(){394 * // executed for all envs395 * });396 *397 * app.configure('stage', function(){398 * // executed staging env399 * });400 *401 * app.configure('stage', 'production', function(){402 * // executed for stage and production403 * });404 *405 * @param {String} env...406 * @param {Function} fn

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 408–412why, one step back
408 * @api public
409 */410411app.configure = function(env, fn){412 var envs = 'all'

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 414–420why, one step back
414 fn = args.pop();
415 if (args.length) envs = args;416 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);417 return this;418};419420/**

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

lines 421–435why, one step back
421 * Listen for connections.
422 *423 * This method takes the same arguments424 * as node's `http.Server#listen()`. 425 *426 * @return {http.Server}427 * @api public428 */429430app.listen = function(){431 var server = http.createServer(this);432 return server.listen.apply(server, arguments);433};434435/**

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 436–441why, one step back
436 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.
437 */438439methods.forEach(function(method){440 app[method] = function(path){441 if ('get' == method && 1 == arguments.length) return this.set(path);

Removed toArray() utilTj Holowaychukbefore adoption7a7d77e

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

fixed auto use(app.router)Tj Holowaychukbefore adoption92c5284

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

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

line 444why, one step back
444 return this._router.route.apply(this._router, args);

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 445–453why, one step back
445 }
446});447448/**449 * Special-cased "all" method, applying the given route `path`,450 * middleware, and callback to _every_ HTTP method.451 *452 * @param {String} path453 * @param {Function} ...

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

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 455–460why, one step back
455 * @api public
456 */457458app.all = function(path){459 var args = arguments;460 methods.forEach(function(method){

backported fix from 2.x for app.all() del methodTj Holowaychukbefore adoptionc3c0fb9

line 461why, one step back
461 if ('all' == method || 'del' == method) return;

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 462–497why, one step back
462 app[method].apply(this, args);
463 }, this);464 return this;465};466467// del -> delete alias468469app.del = app.delete;470471/**472 * Render the given view `name` name with `options`473 * and a callback accepting an error and the474 * rendered template string.475 *476 * @param {String} name477 * @param {String|Function} options or fn478 * @param {Function} fn479 * @api public480 */481482app.render = function(name, options, fn){483 var self = this484 , opts = {}485 , cache = this.cache486 , engines = this.engines487 , view;488489 // support callback function as second arg490 if ('function' == typeof options) {491 fn = options, options = {};492 }493494 // merge app.locals495 utils.merge(opts, this.locals);496497 // merge options.locals

refactored app.render()TJ Holowaychukbefore adoptione2f43df

line 498why, one step back
498 if (options.locals) utils.merge(opts, options.locals);

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 499–513why, one step back
499
500 // merge options501 utils.merge(opts, options);502503 // set .cache unless explicitly provided504 opts.cache = null == opts.cache505 ? this.enabled('view cache')506 : opts.cache;507508 // primed cache509 if (opts.cache) view = cache[name];510511 // view512 if (!view) {513 view = new View(name, {

Fixed app.render() absolute path supportTj Holowaychukbefore adoptionb9dee73

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

Added app.redirect() testsTj Holowaychukbefore adoption11f55a0

lines 516–529why, one step back
516 , engines: engines
517 });518519 // prime the cache520 if (opts.cache) cache[name] = view;521 }522523 // render524 try {525 view.render(opts, fn);526 } catch (err) {527 fn(err);528 }529};