Skip to content
Myr

http.js

This file

Path

lib/http.js

501 lines · 10.0 KiB · JavaScript

Last changed by

Removed `app.error()`. Closes #733Tj Holowaychukbefore adoption456a985

History of this file

Take it

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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;

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

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

more docsTj Holowaychukbefore adoption6230ec5

lines 66–89why, one step back
66 this.dynamicViewHelpers = {};
6768 this.set('home', '/');69 this.set('env', process.env.NODE_ENV || 'development');7071 // expose objects to each other72 this.use(function(req, res, next){73 req.query = req.query || {};74 res.setHeader('X-Powered-By', 'Express');75 req.app = res.app = self;76 req.res = res;77 res.req = req;78 req.next = next;79 // assign req.query80 if (req.url.indexOf('?') > 0) {81 var query = url.parse(req.url).query;82 req.query = qs.parse(query);83 }84 next();85 });8687 // apply middleware88 if (middleware) middleware.forEach(self.use.bind(self));89

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

more docsTj Holowaychukbefore adoption6230ec5

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

more docsTj Holowaychukbefore adoption6230ec5

lines 95–109why, one step back
95 });
9697 // default locals98 this.locals({99 settings: this.settings100 , app: this101 });102103 // default development configuration104 this.configure('development', function(){105 this.enable('hints');106 });107108 // default production configuration109 this.configure('production', function(){

Renamed "cache views" to "view cache". Closes #628Tj Holowaychukbefore adoptiond9e7153

line 110why, one step back
110 this.enable('view cache');

more docsTj Holowaychukbefore adoption6230ec5

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

more docsTj Holowaychukbefore adoption6230ec5

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

more docsTj Holowaychukbefore adoption6230ec5

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

more docsTj Holowaychukbefore adoption6230ec5

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 127–131why, one step back
127
128 // del -> delete129 self.lookup.del = self.lookup.delete;130 self.match.del = self.match.delete;131 self.remove.del = self.remove.delete;

more docsTj Holowaychukbefore adoption6230ec5

lines 132–134why, one step back
132};
133134/**

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 135–171why, one step back
135 * Remove routes matching the given `path`.
136 *137 * @param {Route} path138 * @return {Boolean}139 * @api public140 */141142app.remove = function(path){143 return this.routes.lookup('all', path).remove();144};145146/**147 * Lookup routes defined with a path148 * equivalent to `path`.149 *150 * @param {Stirng} path151 * @return {Array}152 * @api public153 */154155app.lookup = function(path){156 return this.routes.lookup('all', path);157};158159/**160 * Lookup routes matching the given `url`.161 *162 * @param {Stirng} url163 * @return {Array}164 * @api public165 */166167app.match = function(url){168 return this.routes.match('all', url);169};170171/**

more docsTj Holowaychukbefore adoption6230ec5

lines 172–180why, one step back
172 * Proxy `connect.HTTPServer#use()` to apply settings to
173 * mounted applications.174 *175 * @param {String|Function|Server} route176 * @param {Function|Server} middleware177 * @return {Server} for chaining178 * @api public179 */180

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 182–238why, one step back
182 var app, home, handle;
183184 if ('string' != typeof route) {185 middleware = route, route = '/';186 }187188 // express app189 if (middleware.handle && middleware.set) app = middleware;190191 // restore .app property on req and res192 if (app) {193 app.route = route;194 middleware = function(req, res, next) {195 var orig = req.app;196 app.handle(req, res, function(err){197 req.app = res.app = orig;198 next(err);199 });200 };201 }202203 connect.HTTPServer.prototype.use.call(this, route, middleware);204205 // mounted an app, invoke the hook206 // and adjust some settings207 if (app) {208 home = app.set('home');209 if ('/' == home) home = '';210 app.set('home', app.route + home);211 app.parent = this;212 if (app.__mounted) app.__mounted.call(app, this);213 }214215 return this;216};217218/**219 * Assign a callback `fn` which is called220 * when this `Server` is passed to `Server#use()`.221 *222 * Examples:223 *224 * var app = express.createServer()225 * , blog = express.createServer();226 *227 * blog.mounted(function(parent){228 * // parent is app229 * // "this" is blog230 * });231 *232 * app.use(blog);233 *234 * @param {Function} fn235 * @return {Server} for chaining236 * @api public237 */238

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 240–250why, one step back
240 this.__mounted = fn;
241 return this;242};243244/**245 * See: view.register.246 *247 * @return {Server} for chaining248 * @api public249 */250

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 252–264why, one step back
252 view.register.apply(this, arguments);
253 return this;254};255256/**257 * Register the given view helpers `obj`. This method258 * can be called several times to apply additional helpers.259 *260 * @param {Object} obj261 * @return {Server} for chaining262 * @api public263 */264

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

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

more docsTj Holowaychukbefore adoption6230ec5

lines 268–279why, one step back
268 return this;
269};270271/**272 * Register the given dynamic view helpers `obj`. This method273 * can be called several times to apply additional helpers.274 *275 * @param {Object} obj276 * @return {Server} for chaining277 * @api public278 */279

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 281–288why, one step back
281 utils.merge(this.dynamicViewHelpers, obj);
282 return this;283};284285/**286 * Map the given param placeholder `name`(s) to the given callback `fn`.287 *288 * Param mapping is used to provide pre-conditions to routes

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

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

more docsTj Holowaychukbefore adoption6230ec5

line 292why, one step back
292 *

docsTj Holowaychukbefore adoption4d12292

lines 293–304why, one step back
293 * app.param('userId', function(req, res, next, id){
294 * User.find(id, function(err, user){295 * if (err) {296 * next(err);297 * } else if (user) {298 * req.user = user;299 * next();300 * } else {301 * next(new Error('failed to load user'));302 * }303 * });304 * });

more docsTj Holowaychukbefore adoption6230ec5

line 305why, one step back
305 *

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

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

more docsTj Holowaychukbefore adoption6230ec5

lines 307–311why, one step back
307 * @param {Function} fn
308 * @return {Server} for chaining309 * @api public310 */311

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

line 313why, one step back
313 // array

more docsTj Holowaychukbefore adoption6230ec5

lines 314–317why, one step back
314 if (Array.isArray(name)) {
315 name.forEach(function(name){316 this.param(name, fn);317 }, this);

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

lines 318–321why, one step back
318 // param logic
319 } else if ('function' == typeof name) {320 this.routes.param(name);321 // single

more docsTj Holowaychukbefore adoption6230ec5

lines 322–325why, one step back
322 } else {
323 if (':' == name[0]) name = name.substr(1);324 this.routes.param(name, fn);325 }

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

line 326why, one step back
326

more docsTj Holowaychukbefore adoption6230ec5

lines 327–338why, one step back
327 return this;
328};329330/**331 * Register the given callback `fn` for the given `type`.332 *333 * @param {String} type334 * @param {Function} fn335 * @return {Server} for chaining336 * @api public337 */338

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 340–354why, one step back
340 if (!fn) return this.isCallbacks[type];
341 this.isCallbacks[type] = fn;342 return this;343};344345/**346 * Assign `setting` to `val`, or return `setting`'s value.347 * Mounted servers inherit their parent server's settings.348 *349 * @param {String} setting350 * @param {String} val351 * @return {Server|Mixed} for chaining, or the setting value352 * @api public353 */354

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 356–375why, one step back
356 if (val === undefined) {
357 if (this.settings.hasOwnProperty(setting)) {358 return this.settings[setting];359 } else if (this.parent) {360 return this.parent.set(setting);361 }362 } else {363 this.settings[setting] = val;364 return this;365 }366};367368/**369 * Check if `setting` is enabled.370 *371 * @param {String} setting372 * @return {Boolean}373 * @api public374 */375

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 377–387why, one step back
377 return !!this.set(setting);
378};379380/**381 * Check if `setting` is disabled.382 *383 * @param {String} setting384 * @return {Boolean}385 * @api public386 */387

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 389–399why, one step back
389 return !this.set(setting);
390};391392/**393 * Enable `setting`.394 *395 * @param {String} setting396 * @return {Server} for chaining397 * @api public398 */399

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 401–411why, one step back
401 return this.set(setting, true);
402};403404/**405 * Disable `setting`.406 *407 * @param {String} setting408 * @return {Server} for chaining409 * @api public410 */411

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 413–424why, one step back
413 return this.set(setting, false);
414};415416/**417 * Redirect `key` to `url`.418 *419 * @param {String} key420 * @param {String} url421 * @return {Server} for chaining422 * @api public423 */424

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

more docsTj Holowaychukbefore adoption6230ec5

lines 426–430why, one step back
426 this.redirects[key] = url;
427 return this;428};429430/**

docsTj Holowaychukbefore adoptionf46ae9f

lines 431–434why, one step back
431 * Configure callback for zero or more envs,
432 * when no env is specified that callback will433 * be invoked for all environments. Any combination434 * can be used multiple times, in any order desired.

more docsTj Holowaychukbefore adoption6230ec5

line 435why, one step back
435 *

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

lines 436–450why, one step back
436 * Examples:
437 *438 * app.configure(function(){439 * // executed for all envs440 * });441 *442 * app.configure('stage', function(){443 * // executed staging env444 * });445 *446 * app.configure('stage', 'production', function(){447 * // executed for stage and production448 * });449 *450 * @param {String} env...

more docsTj Holowaychukbefore adoption6230ec5

lines 451–455why, one step back
451 * @param {Function} fn
452 * @return {Server} for chaining453 * @api public454 */455

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

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

lines 457–461why, one step back
457 var envs = 'all'
458 , args = toArray(arguments);459 fn = args.pop();460 if (args.length) envs = args;461 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);

more docsTj Holowaychukbefore adoption6230ec5

lines 462–464why, one step back
462 return this;
463};464

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 465–467why, one step back
465/**
466 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.467 */

more docsTj Holowaychukbefore adoption6230ec5

line 468why, one step back
468

misc refactorTj Holowaychukbefore adoption0477a53

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

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

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

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

misc refactorTj Holowaychukbefore adoption0477a53

line 476why, one step back
476});

more docsTj Holowaychukbefore adoption6230ec5

line 477why, one step back
477

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 478–486why, one step back
478/**
479 * Special-cased "all" method, applying the given route `path`,480 * middleware, and callback to _every_ HTTP method.481 *482 * @param {String} path483 * @param {Function} ...484 * @return {Server} for chaining485 * @api public486 */

more docsTj Holowaychukbefore adoption6230ec5

line 487why, one step back
487

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

lines 488–499why, one step back
488app.all = function(path){
489 var args = arguments;490 if (1 == args.length) return this.routes.lookup('all', path);491 methods.forEach(function(method){492 if ('all' == method) return;493 app[method].apply(this, args);494 }, this);495 return this;496};497498// del -> delete alias499

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb

line 501why, one step back
501