Skip to content
Myr

http.js

This file

Path

lib/http.js

544 lines · 10.9 KiB · JavaScript

Last changed by

docsTj Holowaychukbefore adoptionf46ae9f

History of this file

Take 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 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 "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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 "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 19–22why, one step back
19 , url = require('url')
20 , utils = connect.utils;2122/**

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 23–34why, one step back
23 * 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 back
35 * Initialize a new `HTTPServer` with optional `middleware`.
36 *37 * @param {Array} middleware38 * @api public39 */40

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 51–58why, one step back
51
52/**53 * Initialize the server.54 *55 * @param {Array} middleware56 * @api private57 */58

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

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

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 62–64why, one step back
62 this.settings = {};
63 this.redirects = {};64 this.isCallbacks = {};

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

line 65why, one step back
65 this._locals = {};

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 66–90why, one step back
66 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));90

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 112–116why, one step back
112 });
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 back
117

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Merge branch 'feature/route-lookup'Tj Holowaychukbefore adoption260d03a

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 120–121why, one step back
120 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 back
122 };
123

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 126–127why, one step back
126 };
127

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 128–129why, one step back
128 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 back
130 };
131 });

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 132–136why, one step back
132
133 // 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 back
137};
138139/**

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 140–176why, one step back
140 * 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 back
177 * When using the vhost() middleware register error handlers.
178 */179

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 180why, one step back
180app.onvhost = function(){

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 181–190why, one step back
181 this.registerErrorHandlers();
182};183184/**185 * Register error handlers.186 *187 * @return {Server} for chaining188 * @api public189 */190

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 191why, one step back
191app.registerErrorHandlers = function(){

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 269–279why, one step back
269 this.__mounted = fn;
270 return this;271};272273/**274 * See: view.register.275 *276 * @return {Server} for chaining277 * @api public278 */279

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 294–295why, one step back
294app.helpers =
295app.locals = function(obj){

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

line 296why, one step back
296 utils.merge(this._locals, obj);

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 309why, one step back
309app.dynamicHelpers = function(obj){

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

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

lines 318–320why, one step back
318 * which us normalized placeholders. This callback has the same
319 * 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 back
321 *

docsTj Holowaychukbefore adoption4d12292

lines 322–333why, one step back
322 * 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 back
334 *

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

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 336–340why, one step back
336 * @param {Function} fn
337 * @return {Server} for chaining338 * @api public339 */340

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

line 342why, one step back
342 // array

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 343–346why, one step back
343 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 back
347 // param logic
348 } else if ('function' == typeof name) {349 this.routes.param(name);350 // single

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 351–354why, one step back
351 } 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 back
355

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

line 368why, one step back
368app.error = function(fn){

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 420–430why, one step back
420 return !!this.set(setting);
421};422423/**424 * Check if `setting` is disabled.425 *426 * @param {String} setting427 * @return {Boolean}428 * @api public429 */430

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 432–442why, one step back
432 return !this.set(setting);
433};434435/**436 * Enable `setting`.437 *438 * @param {String} setting439 * @return {Server} for chaining440 * @api public441 */442

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 444–454why, one step back
444 return this.set(setting, true);
445};446447/**448 * Disable `setting`.449 *450 * @param {String} setting451 * @return {Server} for chaining452 * @api public453 */454

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

lines 469–473why, one step back
469 this.redirects[key] = url;
470 return this;471};472473/**

docsTj Holowaychukbefore adoptionf46ae9f

lines 474–477why, one step back
474 * 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 back
478 *

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

lines 479–493why, one step back
479 * 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 back
494 * @param {Function} fn
495 * @return {Server} for chaining496 * @api public497 */498

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

lines 500–504why, one step back
500 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 back
505 return this;
506};507

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 508–510why, one step back
508/**
509 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.510 */

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

line 511why, one step back
511

misc refactorTj Holowaychukbefore adoption0477a53

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

misc refactorTj Holowaychukbefore adoption0477a53

line 519why, one step back
519});

Added "request", "response", and "app" localsTj Holowaychukbefore adoption319fbf7

line 520why, one step back
520

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 521–529why, one step back
521/**
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 back
530

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 531–542why, one step back
531app.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 alias542

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 544why, one step back
544