http.js
This file
Path
lib/http.js
504 lines · 10.0 KiB · JavaScript
Last changed by
Changed `app.locals` to match `res.locals`Tj Holowaychukbefore adoption1dc8776
History of this fileTake it
The walk stopped after 200 commits: lines older than 759a57b point at it.
Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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 second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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 adoptionf98896f
line 19why, one step back19 , res = require('./response')Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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/**Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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;Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
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 = {};Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 63–68why, one step back63 this.settings = {};64 this.redirects = {};65 this.isCallbacks = {};6667 this.set('home', '/');68 this.set('env', process.env.NODE_ENV || 'development');utilize connects new `query()` middlewareTj Holowaychukbefore adoptioneab03fd
line 69why, one step back69 this.use(connect.query());Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 70why, one step back70Changed `app.locals` to match `res.locals`Tj Holowaychukbefore adoption1dc8776
lines 71–86why, one step back71 /**72 * Assign several locals with the given `obj`,73 * or return the locals.74 *75 * @param {Object} obj76 * @return {Object|HTTPServer}77 * @api public78 */7980 this.locals = function(obj){81 for (var key in obj) {82 self.locals[key] = obj[key];83 }84 return self;85 };86Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 87–88why, one step back87 // expose objects to each other88 this.use(function(req, res, next){Added "charset" optionTj Holowaychukbefore adoption6ebccb0
line 89why, one step back89 var charset;Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 90–95why, one step back90 req.query = req.query || {};91 res.setHeader('X-Powered-By', 'Express');92 req.app = res.app = self;93 req.res = res;94 res.req = req;95 req.next = next;Added "charset" optionTj Holowaychukbefore adoption6ebccb0
lines 96–99why, one step back9697 // charset98 if (charset = self.set('charset')) res.charset = charset;99Replaced `res.local[s]()` with `res.locals` function. Closes #757Tj Holowaychukbefore adoption9a8b868
lines 100–112why, one step back100 /**101 * Assign several locals with the given `obj`,102 * or return the locals.103 *104 * @param {Object} obj105 * @return {Object|ServerResponse}106 * @api public107 */108109 res.locals = function(obj){110 for (var key in obj) {111 res.locals[key] = obj[key];112 }Changed `app.locals` to match `res.locals`Tj Holowaychukbefore adoption1dc8776
line 113why, one step back113 return res;Replaced `res.local[s]()` with `res.locals` function. Closes #757Tj Holowaychukbefore adoption9a8b868
lines 114–115why, one step back114 };115Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 116–121why, one step back116 next();117 });118119 // apply middleware120 if (middleware) middleware.forEach(self.use.bind(self));121Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 122–123why, one step back122 // router123 this.routes = new Router(this);Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 124–125why, one step back124 this.__defineGetter__('router', function(){125 this.__usedRouter = true;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 126why, one step back126 return self.routes.middleware;Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 127–141why, one step back127 });128129 // default locals130 this.locals({131 settings: this.settings132 , app: this133 });134135 // default development configuration136 this.configure('development', function(){137 this.enable('hints');138 });139140 // default production configuration141 this.configure('production', function(){Renamed "cache views" to "view cache". Closes #628Tj Holowaychukbefore adoptiond9e7153
line 142why, one step back142 this.enable('view cache');Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 143–144why, one step back143 });144Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 145why, one step back145 // route manipulation methodsAdded second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 146why, one step back146 methods.forEach(function(method){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 147–148why, one step back147 self.lookup[method] = function(path){148 return self.routes.lookup(method, path);Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 149–150why, one step back149 };150Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 151–152why, one step back151 self.match[method] = function(path){152 return self.routes.match(method, path);Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 153–154why, one step back153 };154Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 155–156why, one step back155 self.remove[method] = function(path){156 return self.routes.lookup(method, path).remove();Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 157–158why, one step back157 };158 });Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 159–163why, one step back159160 // del -> delete161 self.lookup.del = self.lookup.delete;162 self.match.del = self.match.delete;163 self.remove.del = self.remove.delete;Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 164–166why, one step back164};165166/**Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 167–203why, one step back167 * Remove routes matching the given `path`.168 *169 * @param {Route} path170 * @return {Boolean}171 * @api public172 */173174app.remove = function(path){175 return this.routes.lookup('all', path).remove();176};177178/**179 * Lookup routes defined with a path180 * equivalent to `path`.181 *182 * @param {Stirng} path183 * @return {Array}184 * @api public185 */186187app.lookup = function(path){188 return this.routes.lookup('all', path);189};190191/**192 * Lookup routes matching the given `url`.193 *194 * @param {Stirng} url195 * @return {Array}196 * @api public197 */198199app.match = function(url){200 return this.routes.match('all', url);201};202203/**Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 204–212why, one step back204 * Proxy `connect.HTTPServer#use()` to apply settings to205 * mounted applications.206 *207 * @param {String|Function|Server} route208 * @param {Function|Server} middleware209 * @return {Server} for chaining210 * @api public211 */212Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 213why, one step back213app.use = function(route, middleware){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 214–270why, one step back214 var app, home, handle;215216 if ('string' != typeof route) {217 middleware = route, route = '/';218 }219220 // express app221 if (middleware.handle && middleware.set) app = middleware;222223 // restore .app property on req and res224 if (app) {225 app.route = route;226 middleware = function(req, res, next) {227 var orig = req.app;228 app.handle(req, res, function(err){229 req.app = res.app = orig;230 next(err);231 });232 };233 }234235 connect.HTTPServer.prototype.use.call(this, route, middleware);236237 // mounted an app, invoke the hook238 // and adjust some settings239 if (app) {240 home = app.set('home');241 if ('/' == home) home = '';242 app.set('home', app.route + home);243 app.parent = this;244 if (app.__mounted) app.__mounted.call(app, this);245 }246247 return this;248};249250/**251 * Assign a callback `fn` which is called252 * when this `Server` is passed to `Server#use()`.253 *254 * Examples:255 *256 * var app = express.createServer()257 * , blog = express.createServer();258 *259 * blog.mounted(function(parent){260 * // parent is app261 * // "this" is blog262 * });263 *264 * app.use(blog);265 *266 * @param {Function} fn267 * @return {Server} for chaining268 * @api public269 */270Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 271why, one step back271app.mounted = function(fn){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 272–282why, one step back272 this.__mounted = fn;273 return this;274};275276/**277 * See: view.register.278 *279 * @return {Server} for chaining280 * @api public281 */282Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 283why, one step back283app.register = function(){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 284–291why, one step back284 view.register.apply(this, arguments);285 return this;286};287288/**289 * Map the given param placeholder `name`(s) to the given callback `fn`.290 *291 * Param mapping is used to provide pre-conditions to routesRemoved app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18
lines 292–294why, one step back292 * which us normalized placeholders. This callback has the same293 * signature as regular middleware, for example below when ":userId"294 * is used this function will be invoked in an attempt to load the user.Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 295why, one step back295 *docsTj Holowaychukbefore adoption4d12292
lines 296–307why, one step back296 * app.param('userId', function(req, res, next, id){297 * User.find(id, function(err, user){298 * if (err) {299 * next(err);300 * } else if (user) {301 * req.user = user;302 * next();303 * } else {304 * next(new Error('failed to load user'));305 * }306 * });307 * });Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 308why, one step back308 *Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 309why, one step back309 * @param {String|Array|Function} nameAdded second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 310–314why, one step back310 * @param {Function} fn311 * @return {Server} for chaining312 * @api public313 */314Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 315why, one step back315app.param = function(name, fn){Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 316why, one step back316 // arrayAdded second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 317–320why, one step back317 if (Array.isArray(name)) {318 name.forEach(function(name){319 this.param(name, fn);320 }, this);Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
lines 321–324why, one step back321 // param logic322 } else if ('function' == typeof name) {323 this.routes.param(name);324 // singleAdded second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 325–328why, one step back325 } else {326 if (':' == name[0]) name = name.substr(1);327 this.routes.param(name, fn);328 }Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 329why, one step back329Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 330–341why, one step back330 return this;331};332333/**334 * Register the given callback `fn` for the given `type`.335 *336 * @param {String} type337 * @param {Function} fn338 * @return {Server} for chaining339 * @api public340 */341Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 342why, one step back342app.is = function(type, fn){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 343–357why, one step back343 if (!fn) return this.isCallbacks[type];344 this.isCallbacks[type] = fn;345 return this;346};347348/**349 * Assign `setting` to `val`, or return `setting`'s value.350 * Mounted servers inherit their parent server's settings.351 *352 * @param {String} setting353 * @param {String} val354 * @return {Server|Mixed} for chaining, or the setting value355 * @api public356 */357Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 358why, one step back358app.set = function(setting, val){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 359–378why, one step back359 if (val === undefined) {360 if (this.settings.hasOwnProperty(setting)) {361 return this.settings[setting];362 } else if (this.parent) {363 return this.parent.set(setting);364 }365 } else {366 this.settings[setting] = val;367 return this;368 }369};370371/**372 * Check if `setting` is enabled.373 *374 * @param {String} setting375 * @return {Boolean}376 * @api public377 */378Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 379why, one step back379app.enabled = function(setting){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 380–390why, one step back380 return !!this.set(setting);381};382383/**384 * Check if `setting` is disabled.385 *386 * @param {String} setting387 * @return {Boolean}388 * @api public389 */390Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 391why, one step back391app.disabled = function(setting){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 392–402why, one step back392 return !this.set(setting);393};394395/**396 * Enable `setting`.397 *398 * @param {String} setting399 * @return {Server} for chaining400 * @api public401 */402Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 403why, one step back403app.enable = function(setting){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 404–414why, one step back404 return this.set(setting, true);405};406407/**408 * Disable `setting`.409 *410 * @param {String} setting411 * @return {Server} for chaining412 * @api public413 */414Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 415why, one step back415app.disable = function(setting){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 416–427why, one step back416 return this.set(setting, false);417};418419/**420 * Redirect `key` to `url`.421 *422 * @param {String} key423 * @param {String} url424 * @return {Server} for chaining425 * @api public426 */427Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 428why, one step back428app.redirect = function(key, url){Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 429–433why, one step back429 this.redirects[key] = url;430 return this;431};432433/**docsTj Holowaychukbefore adoptionf46ae9f
lines 434–437why, one step back434 * Configure callback for zero or more envs,435 * when no env is specified that callback will436 * be invoked for all environments. Any combination437 * can be used multiple times, in any order desired.Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 438why, one step back438 *Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 439–453why, one step back439 * Examples:440 *441 * app.configure(function(){442 * // executed for all envs443 * });444 *445 * app.configure('stage', function(){446 * // executed staging env447 * });448 *449 * app.configure('stage', 'production', function(){450 * // executed for stage and production451 * });452 *453 * @param {String} env...Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 454–458why, one step back454 * @param {Function} fn455 * @return {Server} for chaining456 * @api public457 */458Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 459why, one step back459app.configure = function(env, fn){Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 460–464why, one step back460 var envs = 'all'461 , args = toArray(arguments);462 fn = args.pop();463 if (args.length) envs = args;464 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
lines 465–467why, one step back465 return this;466};467Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 468–470why, one step back468/**469 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.470 */Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 471why, one step back471misc refactorTj Holowaychukbefore adoption0477a53
line 472why, one step back472methods.forEach(function(method){Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 473why, one step back473 app[method] = function(path){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 474–475why, one step back474 if (1 == arguments.length) return this.routes.lookup(method, path);475 var args = [method].concat(toArray(arguments));Merge branch 'refactor/route-middleware'Tj Holowaychukbefore adoption06fda62
line 476why, one step back476 if (!this.__usedRouter) this.use(this.router);Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 477–478why, one step back477 return this.routes._route.apply(this.routes, args);478 }misc refactorTj Holowaychukbefore adoption0477a53
line 479why, one step back479});Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 480why, one step back480Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 481–489why, one step back481/**482 * Special-cased "all" method, applying the given route `path`,483 * middleware, and callback to _every_ HTTP method.484 *485 * @param {String} path486 * @param {Function} ...487 * @return {Server} for chaining488 * @api public489 */Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b
line 490why, one step back490Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 491–502why, one step back491app.all = function(path){492 var args = arguments;493 if (1 == args.length) return this.routes.lookup('all', path);494 methods.forEach(function(method){495 if ('all' == method) return;496 app[method].apply(this, args);497 }, this);498 return this;499};500501// del -> delete alias502Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 503why, one step back503app.del = app.delete;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 504why, one step back504