http.js
This file
Path
lib/http.js
544 lines · 10.9 KiB · JavaScript
Last changed by
docsTj Holowaychukbefore adoptionf46ae9f
History of this fileTake it
The walk stopped after 200 commits: lines older than 319fbf7 point at it.
Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
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')Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
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 "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 19–22why, one step back19 , url = require('url')20 , utils = connect.utils;2122/**Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 23–34why, one step back23 * Expose `HTTPServer`.24 */2526exports = module.exports = HTTPServer;2728/**29 * Server proto.30 */3132var app = HTTPServer.prototype;3334/**Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 35–40why, one step back35 * Initialize a new `HTTPServer` with optional `middleware`.36 *37 * @param {Array} middleware38 * @api public39 */40Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 41why, one step back41function HTTPServer(middleware){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 42–49why, one step back42 connect.HTTPServer.call(this, []);43 this.init(middleware);44};4546/**47 * Inherit from `connect.HTTPServer`.48 */49Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 50why, one step back50app.__proto__ = connect.HTTPServer.prototype;Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 51–58why, one step back5152/**53 * Initialize the server.54 *55 * @param {Array} middleware56 * @api private57 */58Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 59why, one step back59app.init = function(middleware){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 60why, one step back60 var self = this;Fixed caching of views when using several apps. Closes #637Tj Holowaychukbefore adoption3d242a6
line 61why, one step back61 this.cache = {};Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 62–64why, one step back62 this.settings = {};63 this.redirects = {};64 this.isCallbacks = {};viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 65why, one step back65 this._locals = {};Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 66–90why, one step back66 this.dynamicViewHelpers = {};67 this.errorHandlers = [];6869 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);Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
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;Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
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');Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 112–116why, one step back112 });113114 // register error handlers on "listening"115 // so that they disregard definition position.116 this.on('listening', this.registerErrorHandlers.bind(this));Merge branch 'feature/route-lookup'Tj Holowaychukbefore adoption260d03a
line 117why, one step back117Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 118why, one step back118 // route manipulation methodsMerge branch 'feature/route-lookup'Tj Holowaychukbefore adoption260d03a
line 119why, one step back119 methods.forEach(function(method){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 120–121why, one step back120 self.lookup[method] = function(path){121 return self.routes.lookup(method, path);Merge branch 'feature/route-lookup'Tj Holowaychukbefore adoption260d03a
lines 122–123why, one step back122 };123Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 124–125why, one step back124 self.match[method] = function(path){125 return self.routes.match(method, path);Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 126–127why, one step back126 };127Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 128–129why, one step back128 self.remove[method] = function(path){129 return self.routes.lookup(method, path).remove();Merge branch 'feature/route-lookup'Tj Holowaychukbefore adoption260d03a
lines 130–131why, one step back130 };131 });Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 132–136why, one step back132133 // del -> delete134 self.lookup.del = self.lookup.delete;135 self.match.del = self.match.delete;136 self.remove.del = self.remove.delete;Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 137–139why, one step back137};138139/**Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 140–176why, one step back140 * Remove routes matching the given `path`.141 *142 * @param {Route} path143 * @return {Boolean}144 * @api public145 */146147app.remove = function(path){148 return this.routes.lookup('all', path).remove();149};150151/**152 * Lookup routes defined with a path153 * equivalent to `path`.154 *155 * @param {Stirng} path156 * @return {Array}157 * @api public158 */159160app.lookup = function(path){161 return this.routes.lookup('all', path);162};163164/**165 * Lookup routes matching the given `url`.166 *167 * @param {Stirng} url168 * @return {Array}169 * @api public170 */171172app.match = function(url){173 return this.routes.match('all', url);174};175176/**Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 177–179why, one step back177 * When using the vhost() middleware register error handlers.178 */179Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 180why, one step back180app.onvhost = function(){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 181–190why, one step back181 this.registerErrorHandlers();182};183184/**185 * Register error handlers.186 *187 * @return {Server} for chaining188 * @api public189 */190Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 191why, one step back191app.registerErrorHandlers = function(){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 192–209why, one step back192 this.errorHandlers.forEach(function(fn){193 this.use(function(err, req, res, next){194 fn.apply(this, arguments);195 });196 }, this);197 return this;198};199200/**201 * Proxy `connect.HTTPServer#use()` to apply settings to202 * mounted applications.203 *204 * @param {String|Function|Server} route205 * @param {Function|Server} middleware206 * @return {Server} for chaining207 * @api public208 */209Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 210why, one step back210app.use = function(route, middleware){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 211–267why, one step back211 var app, home, handle;212213 if ('string' != typeof route) {214 middleware = route, route = '/';215 }216217 // express app218 if (middleware.handle && middleware.set) app = middleware;219220 // restore .app property on req and res221 if (app) {222 app.route = route;223 middleware = function(req, res, next) {224 var orig = req.app;225 app.handle(req, res, function(err){226 req.app = res.app = orig;227 next(err);228 });229 };230 }231232 connect.HTTPServer.prototype.use.call(this, route, middleware);233234 // mounted an app, invoke the hook235 // and adjust some settings236 if (app) {237 home = app.set('home');238 if ('/' == home) home = '';239 app.set('home', app.route + home);240 app.parent = this;241 if (app.__mounted) app.__mounted.call(app, this);242 }243244 return this;245};246247/**248 * Assign a callback `fn` which is called249 * when this `Server` is passed to `Server#use()`.250 *251 * Examples:252 *253 * var app = express.createServer()254 * , blog = express.createServer();255 *256 * blog.mounted(function(parent){257 * // parent is app258 * // "this" is blog259 * });260 *261 * app.use(blog);262 *263 * @param {Function} fn264 * @return {Server} for chaining265 * @api public266 */267Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 268why, one step back268app.mounted = function(fn){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 269–279why, one step back269 this.__mounted = fn;270 return this;271};272273/**274 * See: view.register.275 *276 * @return {Server} for chaining277 * @api public278 */279Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 280why, one step back280app.register = function(){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 281–293why, one step back281 view.register.apply(this, arguments);282 return this;283};284285/**286 * Register the given view helpers `obj`. This method287 * can be called several times to apply additional helpers.288 *289 * @param {Object} obj290 * @return {Server} for chaining291 * @api public292 */293Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 294–295why, one step back294app.helpers =295app.locals = function(obj){viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 296why, one step back296 utils.merge(this._locals, obj);Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 297–308why, one step back297 return this;298};299300/**301 * Register the given dynamic view helpers `obj`. This method302 * can be called several times to apply additional helpers.303 *304 * @param {Object} obj305 * @return {Server} for chaining306 * @api public307 */308Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 309why, one step back309app.dynamicHelpers = function(obj){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 310–317why, one step back310 utils.merge(this.dynamicViewHelpers, obj);311 return this;312};313314/**315 * Map the given param placeholder `name`(s) to the given callback `fn`.316 *317 * Param mapping is used to provide pre-conditions to routesRemoved app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18
lines 318–320why, one step back318 * which us normalized placeholders. This callback has the same319 * signature as regular middleware, for example below when ":userId"320 * is used this function will be invoked in an attempt to load the user.Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 321why, one step back321 *docsTj Holowaychukbefore adoption4d12292
lines 322–333why, one step back322 * app.param('userId', function(req, res, next, id){323 * User.find(id, function(err, user){324 * if (err) {325 * next(err);326 * } else if (user) {327 * req.user = user;328 * next();329 * } else {330 * next(new Error('failed to load user'));331 * }332 * });333 * });Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 334why, one step back334 *Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 335why, one step back335 * @param {String|Array|Function} nameAdded "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 336–340why, one step back336 * @param {Function} fn337 * @return {Server} for chaining338 * @api public339 */340Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 341why, one step back341app.param = function(name, fn){Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 342why, one step back342 // arrayAdded "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 343–346why, one step back343 if (Array.isArray(name)) {344 name.forEach(function(name){345 this.param(name, fn);346 }, this);Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
lines 347–350why, one step back347 // param logic348 } else if ('function' == typeof name) {349 this.routes.param(name);350 // singleAdded "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 351–354why, one step back351 } else {352 if (':' == name[0]) name = name.substr(1);353 this.routes.param(name, fn);354 }Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 355why, one step back355Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 356–367why, one step back356 return this;357};358359/**360 * Assign a custom exception handler callback `fn`.361 * These handlers are always _last_ in the middleware stack.362 *363 * @param {Function} fn364 * @return {Server} for chaining365 * @api public366 */367Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 368why, one step back368app.error = function(fn){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 369–381why, one step back369 this.errorHandlers.push(fn);370 return this;371};372373/**374 * Register the given callback `fn` for the given `type`.375 *376 * @param {String} type377 * @param {Function} fn378 * @return {Server} for chaining379 * @api public380 */381Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 382why, one step back382app.is = function(type, fn){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 383–397why, one step back383 if (!fn) return this.isCallbacks[type];384 this.isCallbacks[type] = fn;385 return this;386};387388/**389 * Assign `setting` to `val`, or return `setting`'s value.390 * Mounted servers inherit their parent server's settings.391 *392 * @param {String} setting393 * @param {String} val394 * @return {Server|Mixed} for chaining, or the setting value395 * @api public396 */397Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 398why, one step back398app.set = function(setting, val){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 399–418why, one step back399 if (val === undefined) {400 if (this.settings.hasOwnProperty(setting)) {401 return this.settings[setting];402 } else if (this.parent) {403 return this.parent.set(setting);404 }405 } else {406 this.settings[setting] = val;407 return this;408 }409};410411/**412 * Check if `setting` is enabled.413 *414 * @param {String} setting415 * @return {Boolean}416 * @api public417 */418Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 419why, one step back419app.enabled = function(setting){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 420–430why, one step back420 return !!this.set(setting);421};422423/**424 * Check if `setting` is disabled.425 *426 * @param {String} setting427 * @return {Boolean}428 * @api public429 */430Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 431why, one step back431app.disabled = function(setting){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 432–442why, one step back432 return !this.set(setting);433};434435/**436 * Enable `setting`.437 *438 * @param {String} setting439 * @return {Server} for chaining440 * @api public441 */442Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 443why, one step back443app.enable = function(setting){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 444–454why, one step back444 return this.set(setting, true);445};446447/**448 * Disable `setting`.449 *450 * @param {String} setting451 * @return {Server} for chaining452 * @api public453 */454Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 455why, one step back455app.disable = function(setting){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 456–467why, one step back456 return this.set(setting, false);457};458459/**460 * Redirect `key` to `url`.461 *462 * @param {String} key463 * @param {String} url464 * @return {Server} for chaining465 * @api public466 */467Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 468why, one step back468app.redirect = function(key, url){Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 469–473why, one step back469 this.redirects[key] = url;470 return this;471};472473/**docsTj Holowaychukbefore adoptionf46ae9f
lines 474–477why, one step back474 * Configure callback for zero or more envs,475 * when no env is specified that callback will476 * be invoked for all environments. Any combination477 * can be used multiple times, in any order desired.Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 478why, one step back478 *Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 479–493why, one step back479 * Examples:480 *481 * app.configure(function(){482 * // executed for all envs483 * });484 *485 * app.configure('stage', function(){486 * // executed staging env487 * });488 *489 * app.configure('stage', 'production', function(){490 * // executed for stage and production491 * });492 *493 * @param {String} env...Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 494–498why, one step back494 * @param {Function} fn495 * @return {Server} for chaining496 * @api public497 */498Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 499why, one step back499app.configure = function(env, fn){Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 500–504why, one step back500 var envs = 'all'501 , args = toArray(arguments);502 fn = args.pop();503 if (args.length) envs = args;504 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 505–507why, one step back505 return this;506};507Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 508–510why, one step back508/**509 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.510 */Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 511why, one step back511misc refactorTj Holowaychukbefore adoption0477a53
line 512why, one step back512methods.forEach(function(method){Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 513why, one step back513 app[method] = function(path){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 514–515why, one step back514 if (1 == arguments.length) return this.routes.lookup(method, path);515 var args = [method].concat(toArray(arguments));Merge branch 'refactor/route-middleware'Tj Holowaychukbefore adoption06fda62
line 516why, one step back516 if (!this.__usedRouter) this.use(this.router);Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 517–518why, one step back517 return this.routes._route.apply(this.routes, args);518 }misc refactorTj Holowaychukbefore adoption0477a53
line 519why, one step back519});Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 520why, one step back520Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 521–529why, one step back521/**522 * Special-cased "all" method, applying the given route `path`,523 * middleware, and callback to _every_ HTTP method.524 *525 * @param {String} path526 * @param {Function} ...527 * @return {Server} for chaining528 * @api public529 */Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
line 530why, one step back530Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 531–542why, one step back531app.all = function(path){532 var args = arguments;533 if (1 == args.length) return this.routes.lookup('all', path);534 methods.forEach(function(method){535 if ('all' == method) return;536 app[method].apply(this, args);537 }, this);538 return this;539};540541// del -> delete alias542Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 543why, one step back543app.del = app.delete;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 544why, one step back544