Skip to content
Myr

http.js

This file

Path

lib/http.js

521 lines · 10.4 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 99b49d2 point at it.

updated docsTj Holowaychukbefore adoption99b49d2

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')

updated docsTj Holowaychukbefore adoption99b49d2

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 adoptiona4db994

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

updated docsTj Holowaychukbefore adoption99b49d2

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/**

updated docsTj Holowaychukbefore adoption99b49d2

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){

updated docsTj Holowaychukbefore adoption99b49d2

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;

updated docsTj Holowaychukbefore adoption99b49d2

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){

updated docsTj Holowaychukbefore adoption99b49d2

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 = {};

updated docsTj Holowaychukbefore adoption99b49d2

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

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

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

`app.dynamicHelpers` -> `app.dynamicLocals`Tj Holowaychukbefore adoption336aaae

line 67why, one step back
67 this._dynamicLocals = {};

updated docsTj Holowaychukbefore adoption99b49d2

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

updated docsTj Holowaychukbefore adoption99b49d2

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;

updated docsTj Holowaychukbefore adoption99b49d2

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');

updated docsTj Holowaychukbefore adoption99b49d2

lines 112–113why, one step back
112 });
113

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

updated docsTj Holowaychukbefore adoption99b49d2

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 116–117why, one step back
116 self.lookup[method] = function(path){
117 return self.routes.lookup(method, path);

updated docsTj Holowaychukbefore adoption99b49d2

lines 118–119why, one step back
118 };
119

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 122–123why, one step back
122 };
123

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

updated docsTj Holowaychukbefore adoption99b49d2

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 128–132why, one step back
128
129 // del -> delete130 self.lookup.del = self.lookup.delete;131 self.match.del = self.match.delete;132 self.remove.del = self.remove.delete;

updated docsTj Holowaychukbefore adoption99b49d2

lines 133–135why, one step back
133};
134135/**

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 136–172why, one step back
136 * Remove routes matching the given `path`.
137 *138 * @param {Route} path139 * @return {Boolean}140 * @api public141 */142143app.remove = function(path){144 return this.routes.lookup('all', path).remove();145};146147/**148 * Lookup routes defined with a path149 * equivalent to `path`.150 *151 * @param {Stirng} path152 * @return {Array}153 * @api public154 */155156app.lookup = function(path){157 return this.routes.lookup('all', path);158};159160/**161 * Lookup routes matching the given `url`.162 *163 * @param {Stirng} url164 * @return {Array}165 * @api public166 */167168app.match = function(url){169 return this.routes.match('all', url);170};171172/**

updated docsTj Holowaychukbefore adoption99b49d2

lines 173–181why, one step back
173 * Proxy `connect.HTTPServer#use()` to apply settings to
174 * mounted applications.175 *176 * @param {String|Function|Server} route177 * @param {Function|Server} middleware178 * @return {Server} for chaining179 * @api public180 */181

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 183–239why, one step back
183 var app, home, handle;
184185 if ('string' != typeof route) {186 middleware = route, route = '/';187 }188189 // express app190 if (middleware.handle && middleware.set) app = middleware;191192 // restore .app property on req and res193 if (app) {194 app.route = route;195 middleware = function(req, res, next) {196 var orig = req.app;197 app.handle(req, res, function(err){198 req.app = res.app = orig;199 next(err);200 });201 };202 }203204 connect.HTTPServer.prototype.use.call(this, route, middleware);205206 // mounted an app, invoke the hook207 // and adjust some settings208 if (app) {209 home = app.set('home');210 if ('/' == home) home = '';211 app.set('home', app.route + home);212 app.parent = this;213 if (app.__mounted) app.__mounted.call(app, this);214 }215216 return this;217};218219/**220 * Assign a callback `fn` which is called221 * when this `Server` is passed to `Server#use()`.222 *223 * Examples:224 *225 * var app = express.createServer()226 * , blog = express.createServer();227 *228 * blog.mounted(function(parent){229 * // parent is app230 * // "this" is blog231 * });232 *233 * app.use(blog);234 *235 * @param {Function} fn236 * @return {Server} for chaining237 * @api public238 */239

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 241–251why, one step back
241 this.__mounted = fn;
242 return this;243};244245/**246 * See: view.register.247 *248 * @return {Server} for chaining249 * @api public250 */251

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 253–257why, one step back
253 view.register.apply(this, arguments);
254 return this;255};256257/**

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

lines 258–259why, one step back
258 * Assign the view local variable `name` to `val` or return the
259 * local previously assigned to `name`.

updated docsTj Holowaychukbefore adoption99b49d2

line 260why, one step back
260 *

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

lines 261–277why, one step back
261 * @param {String} name
262 * @param {Mixed} val263 * @return {Mixed} val264 * @api public265 */266267app.local = function(name, val){268 this._locals = this._locals || {};269 return undefined === val270 ? this._locals[name]271 : this._locals[name] = val;272};273274/**275 * Assign several locals with the given `obj`,276 * or return the locals.277 *

updated docsTj Holowaychukbefore adoption99b49d2

line 278why, one step back
278 * @param {Object} obj

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

line 279why, one step back
279 * @return {Object|HTTPServer}

updated docsTj Holowaychukbefore adoption99b49d2

lines 280–282why, one step back
280 * @api public
281 */282

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

lines 284–287why, one step back
284 if (0 == arguments.length) return this._locals;
285 for (var key in obj) {286 this.local(key, obj[key]);287 }

updated docsTj Holowaychukbefore adoption99b49d2

lines 288–299why, one step back
288 return this;
289};290291/**292 * Register the given dynamic view helpers `obj`. This method293 * can be called several times to apply additional helpers.294 *295 * @param {Object} obj296 * @return {Server} for chaining297 * @api public298 */299

`app.dynamicHelpers` -> `app.dynamicLocals`Tj Holowaychukbefore adoption336aaae

lines 300–301why, one step back
300app.dynamicLocals = function(obj){
301 utils.merge(this._dynamicLocals, obj);

updated docsTj Holowaychukbefore adoption99b49d2

lines 302–308why, one step back
302 return this;
303};304305/**306 * Map the given param placeholder `name`(s) to the given callback `fn`.307 *308 * Param mapping is used to provide pre-conditions to routes

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

lines 309–311why, one step back
309 * which us normalized placeholders. This callback has the same
310 * signature as regular middleware, for example below when ":userId"311 * is used this function will be invoked in an attempt to load the user.

updated docsTj Holowaychukbefore adoption99b49d2

line 312why, one step back
312 *

docsTj Holowaychukbefore adoption4d12292

lines 313–324why, one step back
313 * app.param('userId', function(req, res, next, id){
314 * User.find(id, function(err, user){315 * if (err) {316 * next(err);317 * } else if (user) {318 * req.user = user;319 * next();320 * } else {321 * next(new Error('failed to load user'));322 * }323 * });324 * });

updated docsTj Holowaychukbefore adoption99b49d2

line 325why, one step back
325 *

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

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 327–331why, one step back
327 * @param {Function} fn
328 * @return {Server} for chaining329 * @api public330 */331

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

line 333why, one step back
333 // array

updated docsTj Holowaychukbefore adoption99b49d2

lines 334–337why, one step back
334 if (Array.isArray(name)) {
335 name.forEach(function(name){336 this.param(name, fn);337 }, this);

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

lines 338–341why, one step back
338 // param logic
339 } else if ('function' == typeof name) {340 this.routes.param(name);341 // single

updated docsTj Holowaychukbefore adoption99b49d2

lines 342–345why, one step back
342 } else {
343 if (':' == name[0]) name = name.substr(1);344 this.routes.param(name, fn);345 }

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

line 346why, one step back
346

updated docsTj Holowaychukbefore adoption99b49d2

lines 347–358why, one step back
347 return this;
348};349350/**351 * Register the given callback `fn` for the given `type`.352 *353 * @param {String} type354 * @param {Function} fn355 * @return {Server} for chaining356 * @api public357 */358

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 360–374why, one step back
360 if (!fn) return this.isCallbacks[type];
361 this.isCallbacks[type] = fn;362 return this;363};364365/**366 * Assign `setting` to `val`, or return `setting`'s value.367 * Mounted servers inherit their parent server's settings.368 *369 * @param {String} setting370 * @param {String} val371 * @return {Server|Mixed} for chaining, or the setting value372 * @api public373 */374

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 376–395why, one step back
376 if (val === undefined) {
377 if (this.settings.hasOwnProperty(setting)) {378 return this.settings[setting];379 } else if (this.parent) {380 return this.parent.set(setting);381 }382 } else {383 this.settings[setting] = val;384 return this;385 }386};387388/**389 * Check if `setting` is enabled.390 *391 * @param {String} setting392 * @return {Boolean}393 * @api public394 */395

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 397–407why, one step back
397 return !!this.set(setting);
398};399400/**401 * Check if `setting` is disabled.402 *403 * @param {String} setting404 * @return {Boolean}405 * @api public406 */407

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 409–419why, one step back
409 return !this.set(setting);
410};411412/**413 * Enable `setting`.414 *415 * @param {String} setting416 * @return {Server} for chaining417 * @api public418 */419

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 421–431why, one step back
421 return this.set(setting, true);
422};423424/**425 * Disable `setting`.426 *427 * @param {String} setting428 * @return {Server} for chaining429 * @api public430 */431

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 433–444why, one step back
433 return this.set(setting, false);
434};435436/**437 * Redirect `key` to `url`.438 *439 * @param {String} key440 * @param {String} url441 * @return {Server} for chaining442 * @api public443 */444

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

updated docsTj Holowaychukbefore adoption99b49d2

lines 446–450why, one step back
446 this.redirects[key] = url;
447 return this;448};449450/**

docsTj Holowaychukbefore adoptionf46ae9f

lines 451–454why, one step back
451 * Configure callback for zero or more envs,
452 * when no env is specified that callback will453 * be invoked for all environments. Any combination454 * can be used multiple times, in any order desired.

updated docsTj Holowaychukbefore adoption99b49d2

line 455why, one step back
455 *

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

lines 456–470why, one step back
456 * Examples:
457 *458 * app.configure(function(){459 * // executed for all envs460 * });461 *462 * app.configure('stage', function(){463 * // executed staging env464 * });465 *466 * app.configure('stage', 'production', function(){467 * // executed for stage and production468 * });469 *470 * @param {String} env...

updated docsTj Holowaychukbefore adoption99b49d2

lines 471–475why, one step back
471 * @param {Function} fn
472 * @return {Server} for chaining473 * @api public474 */475

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

lines 477–481why, one step back
477 var envs = 'all'
478 , args = toArray(arguments);479 fn = args.pop();480 if (args.length) envs = args;481 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);

updated docsTj Holowaychukbefore adoption99b49d2

lines 482–484why, one step back
482 return this;
483};484

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 485–487why, one step back
485/**
486 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.487 */

updated docsTj Holowaychukbefore adoption99b49d2

line 488why, one step back
488

misc refactorTj Holowaychukbefore adoption0477a53

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 491–492why, one step back
491 if (1 == arguments.length) return this.routes.lookup(method, path);
492 var args = [method].concat(toArray(arguments));

Merge branch 'refactor/route-middleware'Tj Holowaychukbefore adoption06fda62

line 493why, one step back
493 if (!this.__usedRouter) this.use(this.router);

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

misc refactorTj Holowaychukbefore adoption0477a53

line 496why, one step back
496});

updated docsTj Holowaychukbefore adoption99b49d2

line 497why, one step back
497

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 498–506why, one step back
498/**
499 * Special-cased "all" method, applying the given route `path`,500 * middleware, and callback to _every_ HTTP method.501 *502 * @param {String} path503 * @param {Function} ...504 * @return {Server} for chaining505 * @api public506 */

updated docsTj Holowaychukbefore adoption99b49d2

line 507why, one step back
507

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 508–519why, one step back
508app.all = function(path){
509 var args = arguments;510 if (1 == args.length) return this.routes.lookup('all', path);511 methods.forEach(function(method){512 if ('all' == method) return;513 app[method].apply(this, args);514 }, this);515 return this;516};517518// del -> delete alias519

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 521why, one step back
521