http.js
This file
Path
lib/http.js
524 lines · 10.4 KiB · JavaScript
Last changed by
Removed app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18
History of this fileTake it
The walk stopped after 200 commits: lines older than 4874404 point at it.
typoTj Holowaychukbefore adoption4874404
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')typoTj Holowaychukbefore adoption4874404
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')typoTj Holowaychukbefore adoption4874404
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/**typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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;typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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 = {};typoTj Holowaychukbefore adoption4874404
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 = {};typoTj Holowaychukbefore adoption4874404
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);typoTj Holowaychukbefore adoption4874404
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;typoTj Holowaychukbefore adoption4874404
lines 96–97why, one step back96 });97Added 'settings' local variable, containing the app's settingsTj Holowaychukbefore adoption9d2bd29
line 98why, one step back98 // default localsAdded "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7
lines 99–103why, one step back99 this.locals({100 settings: this.settings101 , app: this102 });103typoTj Holowaychukbefore adoption4874404
lines 104–110why, one step back104 // 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');typoTj Holowaychukbefore adoption4874404
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;typoTj Holowaychukbefore adoption4874404
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/**typoTj Holowaychukbefore adoption4874404
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(){typoTj Holowaychukbefore adoption4874404
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(){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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(){typoTj Holowaychukbefore adoption4874404
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);typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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.typoTj Holowaychukbefore adoption4874404
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 * });typoTj Holowaychukbefore adoption4874404
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} nametypoTj Holowaychukbefore adoption4874404
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 // arraytypoTj Holowaychukbefore adoption4874404
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 // singletypoTj Holowaychukbefore adoption4874404
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 back355typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
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){typoTj Holowaychukbefore adoption4874404
lines 469–481why, one step back469 this.redirects[key] = url;470 return this;471};472473/**474 * Configure callback for the given `env`.475 *476 * @param {String} env477 * @param {Function} fn478 * @return {Server} for chaining479 * @api public480 */481Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 482why, one step back482app.configure = function(env, fn){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 483–484why, one step back483 if ('function' == typeof env) fn = env, env = 'all';484 if ('all' == env || env == this.settings.env) fn.call(this);typoTj Holowaychukbefore adoption4874404
lines 485–487why, one step back485 return this;486};487Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 488–490why, one step back488/**489 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.490 */typoTj Holowaychukbefore adoption4874404
line 491why, one step back491misc refactorTj Holowaychukbefore adoption0477a53
line 492why, one step back492methods.forEach(function(method){Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 493why, one step back493 app[method] = function(path){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 494–495why, one step back494 if (1 == arguments.length) return this.routes.lookup(method, path);495 var args = [method].concat(toArray(arguments));Merge branch 'refactor/route-middleware'Tj Holowaychukbefore adoption06fda62
line 496why, one step back496 if (!this.__usedRouter) this.use(this.router);Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 497–498why, one step back497 return this.routes._route.apply(this.routes, args);498 }misc refactorTj Holowaychukbefore adoption0477a53
line 499why, one step back499});typoTj Holowaychukbefore adoption4874404
line 500why, one step back500Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 501–509why, one step back501/**502 * Special-cased "all" method, applying the given route `path`,503 * middleware, and callback to _every_ HTTP method.504 *505 * @param {String} path506 * @param {Function} ...507 * @return {Server} for chaining508 * @api public509 */typoTj Holowaychukbefore adoption4874404
line 510why, one step back510Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 511–522why, one step back511app.all = function(path){512 var args = arguments;513 if (1 == args.length) return this.routes.lookup('all', path);514 methods.forEach(function(method){515 if ('all' == method) return;516 app[method].apply(this, args);517 }, this);518 return this;519};520521// del -> delete alias522Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 523why, one step back523app.del = app.delete;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 524why, one step back524