Skip to content
Myr

http.js

This file

Path

lib/http.js

536 lines · 10.7 KiB · JavaScript

Last changed by

Added `app.dynamicLocal(name, fn)`Tj Holowaychukbefore adoptionc275d27

History of this file

Take it

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 adoption0815921

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

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

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

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 connect submoduleTj Holowaychukbefore adoptione4d3f23

line 260why, one step back
260 *

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

lines 261–262why, one step back
261 * @param {String} name
262 * @param {Mixed} val

`app.local()` and `res.local()` return for chainingTj Holowaychukbefore adoption7237a3c

line 263why, one step back
263 * @return {HTTPServer} for chaining

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

lines 264–267why, one step back
264 * @api public
265 */266267app.local = function(name, val){

`app.local()` and `res.local()` return for chainingTj Holowaychukbefore adoption7237a3c

lines 268–270why, one step back
268 if (1 == arguments.length) return this._locals[name];
269 this._locals[name] = val;270 return this;

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

lines 271–276why, one step back
271};
272273/**274 * Assign several locals with the given `obj`,275 * or return the locals.276 *

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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

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

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 287–290why, one step back
287 return this;
288};289290/**

Added `app.dynamicLocal(name, fn)`Tj Holowaychukbefore adoptionc275d27

lines 291–292why, one step back
291 * Register the given dynamic view locals `obj`. This method
292 * can be called several times to apply additional locals.

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 293–298why, one step back
293 *
294 * @param {Object} obj295 * @return {Server} for chaining296 * @api public297 */298

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

line 299why, one step back
299app.dynamicLocals = function(obj){

Added `app.dynamicLocal(name, fn)`Tj Holowaychukbefore adoptionc275d27

lines 300–316why, one step back
300 for (var key in obj) {
301 this.dynamicLocal(key, obj[key]);302 }303 return this;304};305306/**307 * Register a dynamic local with `name` and callback `fn(req, res)`.308 *309 * @param {String} name310 * @param {Object} fn311 * @return {Server} for chaining312 * @api public313 */314315app.dynamicLocal = function(name, fn){316 this._dynamicLocals[name] = fn;

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 317–323why, one step back
317 return this;
318};319320/**321 * Map the given param placeholder `name`(s) to the given callback `fn`.322 *323 * Param mapping is used to provide pre-conditions to routes

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

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 327why, one step back
327 *

docsTj Holowaychukbefore adoption4d12292

lines 328–339why, one step back
328 * app.param('userId', function(req, res, next, id){
329 * User.find(id, function(err, user){330 * if (err) {331 * next(err);332 * } else if (user) {333 * req.user = user;334 * next();335 * } else {336 * next(new Error('failed to load user'));337 * }338 * });339 * });

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 340why, one step back
340 *

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

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 342–346why, one step back
342 * @param {Function} fn
343 * @return {Server} for chaining344 * @api public345 */346

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

line 348why, one step back
348 // array

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 349–352why, one step back
349 if (Array.isArray(name)) {
350 name.forEach(function(name){351 this.param(name, fn);352 }, this);

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

lines 353–356why, one step back
353 // param logic
354 } else if ('function' == typeof name) {355 this.routes.param(name);356 // single

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 357–360why, one step back
357 } else {
358 if (':' == name[0]) name = name.substr(1);359 this.routes.param(name, fn);360 }

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

line 361why, one step back
361

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 362–373why, one step back
362 return this;
363};364365/**366 * Register the given callback `fn` for the given `type`.367 *368 * @param {String} type369 * @param {Function} fn370 * @return {Server} for chaining371 * @api public372 */373

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 375–389why, one step back
375 if (!fn) return this.isCallbacks[type];
376 this.isCallbacks[type] = fn;377 return this;378};379380/**381 * Assign `setting` to `val`, or return `setting`'s value.382 * Mounted servers inherit their parent server's settings.383 *384 * @param {String} setting385 * @param {String} val386 * @return {Server|Mixed} for chaining, or the setting value387 * @api public388 */389

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 391–410why, one step back
391 if (val === undefined) {
392 if (this.settings.hasOwnProperty(setting)) {393 return this.settings[setting];394 } else if (this.parent) {395 return this.parent.set(setting);396 }397 } else {398 this.settings[setting] = val;399 return this;400 }401};402403/**404 * Check if `setting` is enabled.405 *406 * @param {String} setting407 * @return {Boolean}408 * @api public409 */410

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 412–422why, one step back
412 return !!this.set(setting);
413};414415/**416 * Check if `setting` is disabled.417 *418 * @param {String} setting419 * @return {Boolean}420 * @api public421 */422

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 424–434why, one step back
424 return !this.set(setting);
425};426427/**428 * Enable `setting`.429 *430 * @param {String} setting431 * @return {Server} for chaining432 * @api public433 */434

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 436–446why, one step back
436 return this.set(setting, true);
437};438439/**440 * Disable `setting`.441 *442 * @param {String} setting443 * @return {Server} for chaining444 * @api public445 */446

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 448–459why, one step back
448 return this.set(setting, false);
449};450451/**452 * Redirect `key` to `url`.453 *454 * @param {String} key455 * @param {String} url456 * @return {Server} for chaining457 * @api public458 */459

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 461–465why, one step back
461 this.redirects[key] = url;
462 return this;463};464465/**

docsTj Holowaychukbefore adoptionf46ae9f

lines 466–469why, one step back
466 * Configure callback for zero or more envs,
467 * when no env is specified that callback will468 * be invoked for all environments. Any combination469 * can be used multiple times, in any order desired.

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 470why, one step back
470 *

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

lines 471–485why, one step back
471 * Examples:
472 *473 * app.configure(function(){474 * // executed for all envs475 * });476 *477 * app.configure('stage', function(){478 * // executed staging env479 * });480 *481 * app.configure('stage', 'production', function(){482 * // executed for stage and production483 * });484 *485 * @param {String} env...

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 486–490why, one step back
486 * @param {Function} fn
487 * @return {Server} for chaining488 * @api public489 */490

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

lines 492–496why, one step back
492 var envs = 'all'
493 , args = toArray(arguments);494 fn = args.pop();495 if (args.length) envs = args;496 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

lines 497–499why, one step back
497 return this;
498};499

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 500–502why, one step back
500/**
501 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.502 */

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 503why, one step back
503

misc refactorTj Holowaychukbefore adoption0477a53

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

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

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

misc refactorTj Holowaychukbefore adoption0477a53

line 511why, one step back
511});

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 512why, one step back
512

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 513–521why, one step back
513/**
514 * Special-cased "all" method, applying the given route `path`,515 * middleware, and callback to _every_ HTTP method.516 *517 * @param {String} path518 * @param {Function} ...519 * @return {Server} for chaining520 * @api public521 */

Updated connect submoduleTj Holowaychukbefore adoptione4d3f23

line 522why, one step back
522

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 523–534why, one step back
523app.all = function(path){
524 var args = arguments;525 if (1 == args.length) return this.routes.lookup('all', path);526 methods.forEach(function(method){527 if ('all' == method) return;528 app[method].apply(this, args);529 }, this);530 return this;531};532533// del -> delete alias534

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 536why, one step back
536