http.js
This file
Path
lib/http.js
521 lines · 10.4 KiB · JavaScript
Last changed by
Added `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
History of this fileTake it
The walk stopped after 200 commits: lines older than 99b49d2 point at it.
updated docsTj Holowaychukbefore adoption99b49d2
lines 1–13why, one step back12/*!3 * Express - HTTPServer4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var qs = require('qs')13 , connect = require('connect')Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
line 14why, one step back14 , router = require('./router')Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 15why, one step back15 , Router = require('./router')updated docsTj Holowaychukbefore adoption99b49d2
line 16why, one step back16 , view = require('./view')Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 17–18why, one step back17 , toArray = require('./utils').toArray18 , methods = router.methods.concat('del', 'all')Added `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
line 19why, one step back19 , res = require('./response')updated docsTj Holowaychukbefore adoption99b49d2
lines 20–23why, one step back20 , url = require('url')21 , utils = connect.utils;2223/**Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 24–35why, one step back24 * Expose `HTTPServer`.25 */2627exports = module.exports = HTTPServer;2829/**30 * Server proto.31 */3233var app = HTTPServer.prototype;3435/**updated docsTj Holowaychukbefore adoption99b49d2
lines 36–41why, one step back36 * Initialize a new `HTTPServer` with optional `middleware`.37 *38 * @param {Array} middleware39 * @api public40 */41Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 42why, one step back42function HTTPServer(middleware){updated docsTj Holowaychukbefore adoption99b49d2
lines 43–50why, one step back43 connect.HTTPServer.call(this, []);44 this.init(middleware);45};4647/**48 * Inherit from `connect.HTTPServer`.49 */50Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 51why, one step back51app.__proto__ = connect.HTTPServer.prototype;updated docsTj Holowaychukbefore adoption99b49d2
lines 52–59why, one step back5253/**54 * Initialize the server.55 *56 * @param {Array} middleware57 * @api private58 */59Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 60why, one step back60app.init = function(middleware){updated docsTj Holowaychukbefore adoption99b49d2
line 61why, one step back61 var self = this;Fixed caching of views when using several apps. Closes #637Tj Holowaychukbefore adoption3d242a6
line 62why, one step back62 this.cache = {};updated docsTj Holowaychukbefore adoption99b49d2
lines 63–65why, one step back63 this.settings = {};64 this.redirects = {};65 this.isCallbacks = {};viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 66why, one step back66 this._locals = {};`app.dynamicHelpers` -> `app.dynamicLocals`Tj Holowaychukbefore adoption336aaae
line 67why, one step back67 this._dynamicLocals = {};updated docsTj Holowaychukbefore adoption99b49d2
lines 68–90why, one step back6869 this.set('home', '/');70 this.set('env', process.env.NODE_ENV || 'development');7172 // expose objects to each other73 this.use(function(req, res, next){74 req.query = req.query || {};75 res.setHeader('X-Powered-By', 'Express');76 req.app = res.app = self;77 req.res = res;78 res.req = req;79 req.next = next;80 // assign req.query81 if (req.url.indexOf('?') > 0) {82 var query = url.parse(req.url).query;83 req.query = qs.parse(query);84 }85 next();86 });8788 // apply middleware89 if (middleware) middleware.forEach(self.use.bind(self));90Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 91–92why, one step back91 // router92 this.routes = new Router(this);updated docsTj Holowaychukbefore adoption99b49d2
lines 93–94why, one step back93 this.__defineGetter__('router', function(){94 this.__usedRouter = true;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 95why, one step back95 return self.routes.middleware;updated docsTj Holowaychukbefore adoption99b49d2
lines 96–110why, one step back96 });9798 // default locals99 this.locals({100 settings: this.settings101 , app: this102 });103104 // default development configuration105 this.configure('development', function(){106 this.enable('hints');107 });108109 // default production configuration110 this.configure('production', function(){Renamed "cache views" to "view cache". Closes #628Tj Holowaychukbefore adoptiond9e7153
line 111why, one step back111 this.enable('view cache');updated docsTj Holowaychukbefore adoption99b49d2
lines 112–113why, one step back112 });113Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 114why, one step back114 // route manipulation methodsupdated docsTj Holowaychukbefore adoption99b49d2
line 115why, one step back115 methods.forEach(function(method){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 116–117why, one step back116 self.lookup[method] = function(path){117 return self.routes.lookup(method, path);updated docsTj Holowaychukbefore adoption99b49d2
lines 118–119why, one step back118 };119Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 120–121why, one step back120 self.match[method] = function(path){121 return self.routes.match(method, path);Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 122–123why, one step back122 };123Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 124–125why, one step back124 self.remove[method] = function(path){125 return self.routes.lookup(method, path).remove();updated docsTj Holowaychukbefore adoption99b49d2
lines 126–127why, one step back126 };127 });Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 128–132why, one step back128129 // del -> delete130 self.lookup.del = self.lookup.delete;131 self.match.del = self.match.delete;132 self.remove.del = self.remove.delete;updated docsTj Holowaychukbefore adoption99b49d2
lines 133–135why, one step back133};134135/**Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 136–172why, one step back136 * Remove routes matching the given `path`.137 *138 * @param {Route} path139 * @return {Boolean}140 * @api public141 */142143app.remove = function(path){144 return this.routes.lookup('all', path).remove();145};146147/**148 * Lookup routes defined with a path149 * equivalent to `path`.150 *151 * @param {Stirng} path152 * @return {Array}153 * @api public154 */155156app.lookup = function(path){157 return this.routes.lookup('all', path);158};159160/**161 * Lookup routes matching the given `url`.162 *163 * @param {Stirng} url164 * @return {Array}165 * @api public166 */167168app.match = function(url){169 return this.routes.match('all', url);170};171172/**updated docsTj Holowaychukbefore adoption99b49d2
lines 173–181why, one step back173 * Proxy `connect.HTTPServer#use()` to apply settings to174 * mounted applications.175 *176 * @param {String|Function|Server} route177 * @param {Function|Server} middleware178 * @return {Server} for chaining179 * @api public180 */181Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 182why, one step back182app.use = function(route, middleware){updated docsTj Holowaychukbefore adoption99b49d2
lines 183–239why, one step back183 var app, home, handle;184185 if ('string' != typeof route) {186 middleware = route, route = '/';187 }188189 // express app190 if (middleware.handle && middleware.set) app = middleware;191192 // restore .app property on req and res193 if (app) {194 app.route = route;195 middleware = function(req, res, next) {196 var orig = req.app;197 app.handle(req, res, function(err){198 req.app = res.app = orig;199 next(err);200 });201 };202 }203204 connect.HTTPServer.prototype.use.call(this, route, middleware);205206 // mounted an app, invoke the hook207 // and adjust some settings208 if (app) {209 home = app.set('home');210 if ('/' == home) home = '';211 app.set('home', app.route + home);212 app.parent = this;213 if (app.__mounted) app.__mounted.call(app, this);214 }215216 return this;217};218219/**220 * Assign a callback `fn` which is called221 * when this `Server` is passed to `Server#use()`.222 *223 * Examples:224 *225 * var app = express.createServer()226 * , blog = express.createServer();227 *228 * blog.mounted(function(parent){229 * // parent is app230 * // "this" is blog231 * });232 *233 * app.use(blog);234 *235 * @param {Function} fn236 * @return {Server} for chaining237 * @api public238 */239Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 240why, one step back240app.mounted = function(fn){updated docsTj Holowaychukbefore adoption99b49d2
lines 241–251why, one step back241 this.__mounted = fn;242 return this;243};244245/**246 * See: view.register.247 *248 * @return {Server} for chaining249 * @api public250 */251Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 252why, one step back252app.register = function(){updated docsTj Holowaychukbefore adoption99b49d2
lines 253–257why, one step back253 view.register.apply(this, arguments);254 return this;255};256257/**Added `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
lines 258–259why, one step back258 * Assign the view local variable `name` to `val` or return the259 * local previously assigned to `name`.updated docsTj Holowaychukbefore adoption99b49d2
line 260why, one step back260 *Added `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
lines 261–277why, one step back261 * @param {String} name262 * @param {Mixed} val263 * @return {Mixed} val264 * @api public265 */266267app.local = function(name, val){268 this._locals = this._locals || {};269 return undefined === val270 ? this._locals[name]271 : this._locals[name] = val;272};273274/**275 * Assign several locals with the given `obj`,276 * or return the locals.277 *updated docsTj Holowaychukbefore adoption99b49d2
line 278why, one step back278 * @param {Object} objAdded `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
line 279why, one step back279 * @return {Object|HTTPServer}updated docsTj Holowaychukbefore adoption99b49d2
lines 280–282why, one step back280 * @api public281 */282Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 283why, one step back283app.locals = function(obj){Added `app.local(name, val)`Tj Holowaychukbefore adoptiona4db994
lines 284–287why, one step back284 if (0 == arguments.length) return this._locals;285 for (var key in obj) {286 this.local(key, obj[key]);287 }updated docsTj Holowaychukbefore adoption99b49d2
lines 288–299why, one step back288 return this;289};290291/**292 * Register the given dynamic view helpers `obj`. This method293 * can be called several times to apply additional helpers.294 *295 * @param {Object} obj296 * @return {Server} for chaining297 * @api public298 */299`app.dynamicHelpers` -> `app.dynamicLocals`Tj Holowaychukbefore adoption336aaae
lines 300–301why, one step back300app.dynamicLocals = function(obj){301 utils.merge(this._dynamicLocals, obj);updated docsTj Holowaychukbefore adoption99b49d2
lines 302–308why, one step back302 return this;303};304305/**306 * Map the given param placeholder `name`(s) to the given callback `fn`.307 *308 * Param mapping is used to provide pre-conditions to routesRemoved app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18
lines 309–311why, one step back309 * which us normalized placeholders. This callback has the same310 * signature as regular middleware, for example below when ":userId"311 * is used this function will be invoked in an attempt to load the user.updated docsTj Holowaychukbefore adoption99b49d2
line 312why, one step back312 *docsTj Holowaychukbefore adoption4d12292
lines 313–324why, one step back313 * app.param('userId', function(req, res, next, id){314 * User.find(id, function(err, user){315 * if (err) {316 * next(err);317 * } else if (user) {318 * req.user = user;319 * next();320 * } else {321 * next(new Error('failed to load user'));322 * }323 * });324 * });updated docsTj Holowaychukbefore adoption99b49d2
line 325why, one step back325 *Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 326why, one step back326 * @param {String|Array|Function} nameupdated docsTj Holowaychukbefore adoption99b49d2
lines 327–331why, one step back327 * @param {Function} fn328 * @return {Server} for chaining329 * @api public330 */331Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 332why, one step back332app.param = function(name, fn){Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 333why, one step back333 // arrayupdated docsTj Holowaychukbefore adoption99b49d2
lines 334–337why, one step back334 if (Array.isArray(name)) {335 name.forEach(function(name){336 this.param(name, fn);337 }, this);Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
lines 338–341why, one step back338 // param logic339 } else if ('function' == typeof name) {340 this.routes.param(name);341 // singleupdated docsTj Holowaychukbefore adoption99b49d2
lines 342–345why, one step back342 } else {343 if (':' == name[0]) name = name.substr(1);344 this.routes.param(name, fn);345 }Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 346why, one step back346updated docsTj Holowaychukbefore adoption99b49d2
lines 347–358why, one step back347 return this;348};349350/**351 * Register the given callback `fn` for the given `type`.352 *353 * @param {String} type354 * @param {Function} fn355 * @return {Server} for chaining356 * @api public357 */358Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 359why, one step back359app.is = function(type, fn){updated docsTj Holowaychukbefore adoption99b49d2
lines 360–374why, one step back360 if (!fn) return this.isCallbacks[type];361 this.isCallbacks[type] = fn;362 return this;363};364365/**366 * Assign `setting` to `val`, or return `setting`'s value.367 * Mounted servers inherit their parent server's settings.368 *369 * @param {String} setting370 * @param {String} val371 * @return {Server|Mixed} for chaining, or the setting value372 * @api public373 */374Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 375why, one step back375app.set = function(setting, val){updated docsTj Holowaychukbefore adoption99b49d2
lines 376–395why, one step back376 if (val === undefined) {377 if (this.settings.hasOwnProperty(setting)) {378 return this.settings[setting];379 } else if (this.parent) {380 return this.parent.set(setting);381 }382 } else {383 this.settings[setting] = val;384 return this;385 }386};387388/**389 * Check if `setting` is enabled.390 *391 * @param {String} setting392 * @return {Boolean}393 * @api public394 */395Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 396why, one step back396app.enabled = function(setting){updated docsTj Holowaychukbefore adoption99b49d2
lines 397–407why, one step back397 return !!this.set(setting);398};399400/**401 * Check if `setting` is disabled.402 *403 * @param {String} setting404 * @return {Boolean}405 * @api public406 */407Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 408why, one step back408app.disabled = function(setting){updated docsTj Holowaychukbefore adoption99b49d2
lines 409–419why, one step back409 return !this.set(setting);410};411412/**413 * Enable `setting`.414 *415 * @param {String} setting416 * @return {Server} for chaining417 * @api public418 */419Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 420why, one step back420app.enable = function(setting){updated docsTj Holowaychukbefore adoption99b49d2
lines 421–431why, one step back421 return this.set(setting, true);422};423424/**425 * Disable `setting`.426 *427 * @param {String} setting428 * @return {Server} for chaining429 * @api public430 */431Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 432why, one step back432app.disable = function(setting){updated docsTj Holowaychukbefore adoption99b49d2
lines 433–444why, one step back433 return this.set(setting, false);434};435436/**437 * Redirect `key` to `url`.438 *439 * @param {String} key440 * @param {String} url441 * @return {Server} for chaining442 * @api public443 */444Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 445why, one step back445app.redirect = function(key, url){updated docsTj Holowaychukbefore adoption99b49d2
lines 446–450why, one step back446 this.redirects[key] = url;447 return this;448};449450/**docsTj Holowaychukbefore adoptionf46ae9f
lines 451–454why, one step back451 * Configure callback for zero or more envs,452 * when no env is specified that callback will453 * be invoked for all environments. Any combination454 * can be used multiple times, in any order desired.updated docsTj Holowaychukbefore adoption99b49d2
line 455why, one step back455 *Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 456–470why, one step back456 * Examples:457 *458 * app.configure(function(){459 * // executed for all envs460 * });461 *462 * app.configure('stage', function(){463 * // executed staging env464 * });465 *466 * app.configure('stage', 'production', function(){467 * // executed for stage and production468 * });469 *470 * @param {String} env...updated docsTj Holowaychukbefore adoption99b49d2
lines 471–475why, one step back471 * @param {Function} fn472 * @return {Server} for chaining473 * @api public474 */475Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 476why, one step back476app.configure = function(env, fn){Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 477–481why, one step back477 var envs = 'all'478 , args = toArray(arguments);479 fn = args.pop();480 if (args.length) envs = args;481 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);updated docsTj Holowaychukbefore adoption99b49d2
lines 482–484why, one step back482 return this;483};484Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 485–487why, one step back485/**486 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.487 */updated docsTj Holowaychukbefore adoption99b49d2
line 488why, one step back488misc refactorTj Holowaychukbefore adoption0477a53
line 489why, one step back489methods.forEach(function(method){Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 490why, one step back490 app[method] = function(path){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 491–492why, one step back491 if (1 == arguments.length) return this.routes.lookup(method, path);492 var args = [method].concat(toArray(arguments));Merge branch 'refactor/route-middleware'Tj Holowaychukbefore adoption06fda62
line 493why, one step back493 if (!this.__usedRouter) this.use(this.router);Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 494–495why, one step back494 return this.routes._route.apply(this.routes, args);495 }misc refactorTj Holowaychukbefore adoption0477a53
line 496why, one step back496});updated docsTj Holowaychukbefore adoption99b49d2
line 497why, one step back497Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 498–506why, one step back498/**499 * Special-cased "all" method, applying the given route `path`,500 * middleware, and callback to _every_ HTTP method.501 *502 * @param {String} path503 * @param {Function} ...504 * @return {Server} for chaining505 * @api public506 */updated docsTj Holowaychukbefore adoption99b49d2
line 507why, one step back507Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 508–519why, one step back508app.all = function(path){509 var args = arguments;510 if (1 == args.length) return this.routes.lookup('all', path);511 methods.forEach(function(method){512 if ('all' == method) return;513 app[method].apply(this, args);514 }, this);515 return this;516};517518// del -> delete alias519Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 520why, one step back520app.del = app.delete;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 521why, one step back521