Skip to content
Myr

http.js

This file

Path

lib/http.js

500 lines · 10.1 KiB · JavaScript

Last changed by

misc refactorTj Holowaychukbefore adoption0477a53

History of this file

Take it

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

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

misc refactorTj Holowaychukbefore adoption0477a53

line 15why, one step back
15 , methods = router.methods.concat('del', 'all')

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 16–20why, one step back
16 , view = require('./view')
17 , url = require('url')18 , utils = connect.utils;1920/**

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 21–32why, one step back
21 * Expose `HTTPServer`.
22 */2324exports = module.exports = HTTPServer;2526/**27 * Server proto.28 */2930var app = HTTPServer.prototype;3132/**

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 33–38why, one step back
33 * Initialize a new `HTTPServer` with optional `middleware`.
34 *35 * @param {Array} middleware36 * @api public37 */38

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 40–47why, one step back
40 connect.HTTPServer.call(this, []);
41 this.init(middleware);42};4344/**45 * Inherit from `connect.HTTPServer`.46 */47

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 49–56why, one step back
49
50/**51 * Initialize the server.52 *53 * @param {Array} middleware54 * @api private55 */56

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

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

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

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

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

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 64–89why, one step back
64 this.dynamicViewHelpers = {};
65 this.errorHandlers = [];6667 this.set('home', '/');68 this.set('env', process.env.NODE_ENV || 'development');6970 // expose objects to each other71 this.use(function(req, res, next){72 req.query = req.query || {};73 res.setHeader('X-Powered-By', 'Express');74 req.app = res.app = self;75 req.res = res;76 res.req = req;77 req.next = next;78 // assign req.query79 if (req.url.indexOf('?') > 0) {80 var query = url.parse(req.url).query;81 req.query = qs.parse(query);82 }83 next();84 });8586 // apply middleware87 if (middleware) middleware.forEach(self.use.bind(self));8889 // use router, expose as app.get(), etc

Added "case sensitive routes" option.Tj Holowaychukbefore adoptiond2adcbd

line 90why, one step back
90 var fn = router(function(app){ self.routes = app; }, this);

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 91–95why, one step back
91 this.__defineGetter__('router', function(){
92 this.__usedRouter = true;93 return fn;94 });95

Added 'settings' local variable, containing the app's settingsTj Holowaychukbefore adoption9d2bd29

line 96why, one step back
96 // default locals

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

lines 97–101why, one step back
97 this.locals({
98 settings: this.settings99 , app: this100 });101

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

lines 102–106why, one step back
102 // default development configuration
103 this.configure('development', function(){104 this.enable('hints');105 });106

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 107–108why, one step back
107 // default production configuration
108 this.configure('production', function(){

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

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 110–114why, one step back
110 });
111112 // register error handlers on "listening"113 // so that they disregard definition position.114 this.on('listening', this.registerErrorHandlers.bind(this));

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

lines 115–116why, one step back
115
116 // route lookup methods

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 117–128why, one step back
117 this.remove = function(url){
118 return self.remove.all(url);119 };120121 this.match = function(url){122 return self.match.all(url);123 };124125 this.lookup = function(url){126 return self.lookup.all(url);127 };128

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

lines 129–135why, one step back
129 methods.forEach(function(method){
130 self.match[method] = function(url){131 return self.router.match(url, 'all' == method132 ? null133 : method);134 };135

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

lines 136–141why, one step back
136 self.remove[method] = function(url){
137 return self.router.remove(url, 'all' == method138 ? null139 : method);140 };141

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

lines 142–147why, one step back
142 self.lookup[method] = function(path){
143 return self.router.lookup(path, 'all' == method144 ? null145 : method);146 };147 });

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 148–153why, one step back
148};
149150/**151 * When using the vhost() middleware register error handlers.152 */153

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 155–164why, one step back
155 this.registerErrorHandlers();
156};157158/**159 * Register error handlers.160 *161 * @return {Server} for chaining162 * @api public163 */164

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 166–183why, one step back
166 this.errorHandlers.forEach(function(fn){
167 this.use(function(err, req, res, next){168 fn.apply(this, arguments);169 });170 }, this);171 return this;172};173174/**175 * Proxy `connect.HTTPServer#use()` to apply settings to176 * mounted applications.177 *178 * @param {String|Function|Server} route179 * @param {Function|Server} middleware180 * @return {Server} for chaining181 * @api public182 */183

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Merge branch 'integration'Tj Holowaychukbefore adoptionf3c068a

line 185why, one step back
185 var app, home, handle;

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 186–190why, one step back
186
187 if ('string' != typeof route) {188 middleware = route, route = '/';189 }190

Merge branch 'integration'Tj Holowaychukbefore adoptionf3c068a

lines 191–205why, one step back
191 // express app
192 if (middleware.handle && middleware.set) app = middleware;193194 // restore .app property on req and res195 if (app) {196 app.route = route;197 middleware = function(req, res, next) {198 var orig = req.app;199 app.handle(req, res, function(err){200 req.app = res.app = orig;201 next(err);202 });203 };204 }205

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 206–207why, one step back
206 connect.HTTPServer.prototype.use.call(this, route, middleware);
207

Merge branch 'integration'Tj Holowaychukbefore adoptionf3c068a

lines 208–214why, one step back
208 // mounted an app, invoke the hook
209 // and adjust some settings210 if (app) {211 home = app.set('home');212 if ('/' == home) home = '';213 app.set('home', app.route + home);214 app.parent = this;

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 215–241why, one step back
215 if (app.__mounted) app.__mounted.call(app, this);
216 }217218 return this;219};220221/**222 * Assign a callback `fn` which is called223 * when this `Server` is passed to `Server#use()`.224 *225 * Examples:226 *227 * var app = express.createServer()228 * , blog = express.createServer();229 *230 * blog.mounted(function(parent){231 * // parent is app232 * // "this" is blog233 * });234 *235 * app.use(blog);236 *237 * @param {Function} fn238 * @return {Server} for chaining239 * @api public240 */241

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 243–253why, one step back
243 this.__mounted = fn;
244 return this;245};246247/**248 * See: view.register.249 *250 * @return {Server} for chaining251 * @api public252 */253

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 255–267why, one step back
255 view.register.apply(this, arguments);
256 return this;257};258259/**260 * Register the given view helpers `obj`. This method261 * can be called several times to apply additional helpers.262 *263 * @param {Object} obj264 * @return {Server} for chaining265 * @api public266 */267

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 271–282why, one step back
271 return this;
272};273274/**275 * Register the given dynamic view helpers `obj`. This method276 * can be called several times to apply additional helpers.277 *278 * @param {Object} obj279 * @return {Server} for chaining280 * @api public281 */282

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 284–292why, one step back
284 utils.merge(this.dynamicViewHelpers, obj);
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 routes292 * which us normalized placeholders. For example ":user_id" may

Merge branch 'integration'Tj Holowaychukbefore adoptionf3c068a

line 293why, one step back
293 * attempt to load the user from the database, where as ":num" may

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 294–301why, one step back
294 * pass the value through `parseInt(num, 10)`.
295 *296 * When the callback function accepts only a single argument, the297 * value of placeholder is passed:298 *299 * app.param('page', function(n){ return parseInt(n, 10); });300 *301 * After which "/users/:page" would automatically provide us with

Merge branch 'integration'Tj Holowaychukbefore adoptionf3c068a

line 302why, one step back
302 * an integer for `req.params.page`. If desired we could use the callback

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 303–330why, one step back
303 * signature shown below, and immediately `next(new Error('invalid page'))`
304 * when `parseInt` fails.305 *306 * Alternatively the callback may accept the request, response, next, and307 * the value, acting like middlware:308 *309 * app.param('userId', function(req, res, next, id){310 * User.find(id, function(err, user){311 * if (err) {312 * next(err);313 * } else if (user) {314 * req.user = user;315 * next();316 * } else {317 * next(new Error('failed to load user'));318 * }319 * });320 * });321 *322 * Now every time ":userId" is present, the associated user object323 * will be loaded and assigned before the route handler is invoked.324 *325 * @param {String|Array} name326 * @param {Function} fn327 * @return {Server} for chaining328 * @api public329 */330

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 332–351why, one step back
332 if (Array.isArray(name)) {
333 name.forEach(function(name){334 this.param(name, fn);335 }, this);336 } else {337 if (':' == name[0]) name = name.substr(1);338 this.routes.param(name, fn);339 }340 return this;341};342343/**344 * Assign a custom exception handler callback `fn`.345 * These handlers are always _last_ in the middleware stack.346 *347 * @param {Function} fn348 * @return {Server} for chaining349 * @api public350 */351

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 353–365why, one step back
353 this.errorHandlers.push(fn);
354 return this;355};356357/**358 * Register the given callback `fn` for the given `type`.359 *360 * @param {String} type361 * @param {Function} fn362 * @return {Server} for chaining363 * @api public364 */365

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 367–381why, one step back
367 if (!fn) return this.isCallbacks[type];
368 this.isCallbacks[type] = fn;369 return this;370};371372/**373 * Assign `setting` to `val`, or return `setting`'s value.374 * Mounted servers inherit their parent server's settings.375 *376 * @param {String} setting377 * @param {String} val378 * @return {Server|Mixed} for chaining, or the setting value379 * @api public380 */381

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 383–402why, one step back
383 if (val === undefined) {
384 if (this.settings.hasOwnProperty(setting)) {385 return this.settings[setting];386 } else if (this.parent) {387 return this.parent.set(setting);388 }389 } else {390 this.settings[setting] = val;391 return this;392 }393};394395/**396 * Check if `setting` is enabled.397 *398 * @param {String} setting399 * @return {Boolean}400 * @api public401 */402

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 404–414why, one step back
404 return !!this.set(setting);
405};406407/**408 * Check if `setting` is disabled.409 *410 * @param {String} setting411 * @return {Boolean}412 * @api public413 */414

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 416–426why, one step back
416 return !this.set(setting);
417};418419/**420 * Enable `setting`.421 *422 * @param {String} setting423 * @return {Server} for chaining424 * @api public425 */426

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 428–438why, one step back
428 return this.set(setting, true);
429};430431/**432 * Disable `setting`.433 *434 * @param {String} setting435 * @return {Server} for chaining436 * @api public437 */438

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 440–451why, one step back
440 return this.set(setting, false);
441};442443/**444 * Redirect `key` to `url`.445 *446 * @param {String} key447 * @param {String} url448 * @return {Server} for chaining449 * @api public450 */451

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 453–465why, one step back
453 this.redirects[key] = url;
454 return this;455};456457/**458 * Configure callback for the given `env`.459 *460 * @param {String} env461 * @param {Function} fn462 * @return {Server} for chaining463 * @api public464 */465

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 467–477why, one step back
467 if ('function' == typeof env) {
468 fn = env, env = 'all';469 }470 if ('all' == env || env == this.settings.env) {471 fn.call(this);472 }473 return this;474};475476// Generate routing methods477

misc refactorTj Holowaychukbefore adoption0477a53

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

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

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

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

lines 481–487why, one step back
481
482 // Lookup483 if (1 == arguments.length) {484 return this.router.lookup(path, 'all' == method485 ? null486 : method);487 }

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 488–489why, one step back
488
489 // Ensure router is mounted

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

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

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 491–492why, one step back
491
492 // Generate the route

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

line 493why, one step back
493 this.routes[method].apply(this, arguments);

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 494–495why, one step back
494 return this;
495 };

misc refactorTj Holowaychukbefore adoption0477a53

line 496why, one step back
496});

Add haml back in docsTj Holowaychukbefore adoptione85752c

lines 497–499why, one step back
497
498// Alias delete as "del"499

Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab

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