Skip to content
Myr

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 file

Take 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 back
1
2/*!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 back
14 , router = require('./router')

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 15why, one step back
15 , Router = require('./router')

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 16why, one step back
16 , view = require('./view')

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 17–18why, one step back
17 , toArray = require('./utils').toArray
18 , methods = router.methods.concat('del', 'all')

Added `app.local(name, val)`Tj Holowaychukbefore adoptionf98896f

line 19why, one step back
19 , res = require('./response')

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 20–23why, one step back
20 , url = require('url')
21 , utils = connect.utils;2223/**

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 24–35why, one step back
24 * 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 back
36 * Initialize a new `HTTPServer` with optional `middleware`.
37 *38 * @param {Array} middleware39 * @api public40 */41

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 42why, one step back
42function HTTPServer(middleware){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 43–50why, one step back
43 connect.HTTPServer.call(this, []);
44 this.init(middleware);45};4647/**48 * Inherit from `connect.HTTPServer`.49 */50

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 51why, one step back
51app.__proto__ = connect.HTTPServer.prototype;

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 52–59why, one step back
52
53/**54 * Initialize the server.55 *56 * @param {Array} middleware57 * @api private58 */59

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 60why, one step back
60app.init = function(middleware){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 61why, one step back
61 var self = this;

Fixed caching of views when using several apps. Closes #637Tj Holowaychukbefore adoption3d242a6

line 62why, one step back
62 this.cache = {};

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 63–68why, one step back
63 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 back
69 this.use(connect.query());

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 70why, one step back
70

Changed `app.locals` to match `res.locals`Tj Holowaychukbefore adoption1dc8776

lines 71–86why, one step back
71 /**
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 };86

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 87–88why, one step back
87 // expose objects to each other
88 this.use(function(req, res, next){

Added "charset" optionTj Holowaychukbefore adoption6ebccb0

line 89why, one step back
89 var charset;

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 90–95why, one step back
90 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 back
96
97 // charset98 if (charset = self.set('charset')) res.charset = charset;99

Replaced `res.local[s]()` with `res.locals` function. Closes #757Tj Holowaychukbefore adoption9a8b868

lines 100–112why, one step back
100 /**
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 back
113 return res;

Replaced `res.local[s]()` with `res.locals` function. Closes #757Tj Holowaychukbefore adoption9a8b868

lines 114–115why, one step back
114 };
115

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 116–121why, one step back
116 next();
117 });118119 // apply middleware120 if (middleware) middleware.forEach(self.use.bind(self));121

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 122–123why, one step back
122 // router
123 this.routes = new Router(this);

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 124–125why, one step back
124 this.__defineGetter__('router', function(){
125 this.__usedRouter = true;

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 126why, one step back
126 return self.routes.middleware;

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 127–141why, one step back
127 });
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 back
142 this.enable('view cache');

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 143–144why, one step back
143 });
144

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 145why, one step back
145 // route manipulation methods

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 146why, one step back
146 methods.forEach(function(method){

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 147–148why, one step back
147 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 back
149 };
150

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 151–152why, one step back
151 self.match[method] = function(path){
152 return self.routes.match(method, path);

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 153–154why, one step back
153 };
154

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 155–156why, one step back
155 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 back
157 };
158 });

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 159–163why, one step back
159
160 // 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 back
164};
165166/**

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 167–203why, one step back
167 * 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 back
204 * Proxy `connect.HTTPServer#use()` to apply settings to
205 * mounted applications.206 *207 * @param {String|Function|Server} route208 * @param {Function|Server} middleware209 * @return {Server} for chaining210 * @api public211 */212

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 213why, one step back
213app.use = function(route, middleware){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 214–270why, one step back
214 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 */270

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 271why, one step back
271app.mounted = function(fn){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 272–282why, one step back
272 this.__mounted = fn;
273 return this;274};275276/**277 * See: view.register.278 *279 * @return {Server} for chaining280 * @api public281 */282

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 283why, one step back
283app.register = function(){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 284–291why, one step back
284 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 routes

Removed app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18

lines 292–294why, one step back
292 * which us normalized placeholders. This callback has the same
293 * 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 back
295 *

docsTj Holowaychukbefore adoption4d12292

lines 296–307why, one step back
296 * 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 back
308 *

Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310

line 309why, one step back
309 * @param {String|Array|Function} name

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 310–314why, one step back
310 * @param {Function} fn
311 * @return {Server} for chaining312 * @api public313 */314

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 315why, one step back
315app.param = function(name, fn){

Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310

line 316why, one step back
316 // array

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 317–320why, one step back
317 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 back
321 // param logic
322 } else if ('function' == typeof name) {323 this.routes.param(name);324 // single

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 325–328why, one step back
325 } 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 back
329

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 330–341why, one step back
330 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 */341

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 342why, one step back
342app.is = function(type, fn){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 343–357why, one step back
343 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 */357

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 358why, one step back
358app.set = function(setting, val){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 359–378why, one step back
359 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 */378

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 379why, one step back
379app.enabled = function(setting){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 380–390why, one step back
380 return !!this.set(setting);
381};382383/**384 * Check if `setting` is disabled.385 *386 * @param {String} setting387 * @return {Boolean}388 * @api public389 */390

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 391why, one step back
391app.disabled = function(setting){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 392–402why, one step back
392 return !this.set(setting);
393};394395/**396 * Enable `setting`.397 *398 * @param {String} setting399 * @return {Server} for chaining400 * @api public401 */402

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 403why, one step back
403app.enable = function(setting){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 404–414why, one step back
404 return this.set(setting, true);
405};406407/**408 * Disable `setting`.409 *410 * @param {String} setting411 * @return {Server} for chaining412 * @api public413 */414

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 415why, one step back
415app.disable = function(setting){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 416–427why, one step back
416 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 */427

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 428why, one step back
428app.redirect = function(key, url){

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

lines 429–433why, one step back
429 this.redirects[key] = url;
430 return this;431};432433/**

docsTj Holowaychukbefore adoptionf46ae9f

lines 434–437why, one step back
434 * 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 back
438 *

Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344

lines 439–453why, one step back
439 * 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 back
454 * @param {Function} fn
455 * @return {Server} for chaining456 * @api public457 */458

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 459why, one step back
459app.configure = function(env, fn){

Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344

lines 460–464why, one step back
460 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 back
465 return this;
466};467

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 468–470why, one step back
468/**
469 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.470 */

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 471why, one step back
471

misc refactorTj Holowaychukbefore adoption0477a53

line 472why, one step back
472methods.forEach(function(method){

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 473why, one step back
473 app[method] = function(path){

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 474–475why, one step back
474 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 back
476 if (!this.__usedRouter) this.use(this.router);

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 477–478why, one step back
477 return this.routes._route.apply(this.routes, args);
478 }

misc refactorTj Holowaychukbefore adoption0477a53

line 479why, one step back
479});

Added second callback support for res.download() connection errorsTj Holowaychukbefore adoption759a57b

line 480why, one step back
480

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 481–489why, one step back
481/**
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 back
490

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 491–502why, one step back
491app.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 alias502

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 503why, one step back
503app.del = app.delete;

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 504why, one step back
504