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 fileTake it
The walk stopped after 200 commits: lines older than 6230ec5 point at it.
more docsTj Holowaychukbefore adoption6230ec5
lines 1–13why, one step back12/*!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 back14 , router = require('./router')Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 15why, one step back15 , Router = require('./router')more docsTj Holowaychukbefore adoption6230ec5
line 16why, one step back16 , view = require('./view')Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 17–18why, one step back17 , toArray = require('./utils').toArray18 , methods = router.methods.concat('del', 'all')more docsTj Holowaychukbefore adoption6230ec5
lines 19–22why, one step back19 , url = require('url')20 , utils = connect.utils;2122/**Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 23–34why, one step back23 * 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 back35 * Initialize a new `HTTPServer` with optional `middleware`.36 *37 * @param {Array} middleware38 * @api public39 */40Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 41why, one step back41function HTTPServer(middleware){more docsTj Holowaychukbefore adoption6230ec5
lines 42–49why, one step back42 connect.HTTPServer.call(this, []);43 this.init(middleware);44};4546/**47 * Inherit from `connect.HTTPServer`.48 */49Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 50why, one step back50app.__proto__ = connect.HTTPServer.prototype;more docsTj Holowaychukbefore adoption6230ec5
lines 51–58why, one step back5152/**53 * Initialize the server.54 *55 * @param {Array} middleware56 * @api private57 */58Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 59why, one step back59app.init = function(middleware){more docsTj Holowaychukbefore adoption6230ec5
line 60why, one step back60 var self = this;Fixed caching of views when using several apps. Closes #637Tj Holowaychukbefore adoption3d242a6
line 61why, one step back61 this.cache = {};more docsTj Holowaychukbefore adoption6230ec5
lines 62–64why, one step back62 this.settings = {};63 this.redirects = {};64 this.isCallbacks = {};viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 65why, one step back65 this._locals = {};more docsTj Holowaychukbefore adoption6230ec5
lines 66–89why, one step back66 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));89Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 90–91why, one step back90 // router91 this.routes = new Router(this);more docsTj Holowaychukbefore adoption6230ec5
lines 92–93why, one step back92 this.__defineGetter__('router', function(){93 this.__usedRouter = true;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 94why, one step back94 return self.routes.middleware;more docsTj Holowaychukbefore adoption6230ec5
lines 95–109why, one step back95 });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 back110 this.enable('view cache');more docsTj Holowaychukbefore adoption6230ec5
lines 111–112why, one step back111 });112Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 113why, one step back113 // route manipulation methodsmore docsTj Holowaychukbefore adoption6230ec5
line 114why, one step back114 methods.forEach(function(method){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 115–116why, one step back115 self.lookup[method] = function(path){116 return self.routes.lookup(method, path);more docsTj Holowaychukbefore adoption6230ec5
lines 117–118why, one step back117 };118Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 119–120why, one step back119 self.match[method] = function(path){120 return self.routes.match(method, path);Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 121–122why, one step back121 };122Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 123–124why, one step back123 self.remove[method] = function(path){124 return self.routes.lookup(method, path).remove();more docsTj Holowaychukbefore adoption6230ec5
lines 125–126why, one step back125 };126 });Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 127–131why, one step back127128 // 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 back132};133134/**Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 135–171why, one step back135 * 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 back172 * Proxy `connect.HTTPServer#use()` to apply settings to173 * mounted applications.174 *175 * @param {String|Function|Server} route176 * @param {Function|Server} middleware177 * @return {Server} for chaining178 * @api public179 */180Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 181why, one step back181app.use = function(route, middleware){more docsTj Holowaychukbefore adoption6230ec5
lines 182–238why, one step back182 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 */238Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 239why, one step back239app.mounted = function(fn){more docsTj Holowaychukbefore adoption6230ec5
lines 240–250why, one step back240 this.__mounted = fn;241 return this;242};243244/**245 * See: view.register.246 *247 * @return {Server} for chaining248 * @api public249 */250Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 251why, one step back251app.register = function(){more docsTj Holowaychukbefore adoption6230ec5
lines 252–264why, one step back252 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 */264Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 265–266why, one step back265app.helpers =266app.locals = function(obj){viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 267why, one step back267 utils.merge(this._locals, obj);more docsTj Holowaychukbefore adoption6230ec5
lines 268–279why, one step back268 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 */279Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 280why, one step back280app.dynamicHelpers = function(obj){more docsTj Holowaychukbefore adoption6230ec5
lines 281–288why, one step back281 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 routesRemoved app.param() callback with retval supportTj Holowaychukbefore adoptione78dc18
lines 289–291why, one step back289 * which us normalized placeholders. This callback has the same290 * 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 back292 *docsTj Holowaychukbefore adoption4d12292
lines 293–304why, one step back293 * 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 back305 *Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 306why, one step back306 * @param {String|Array|Function} namemore docsTj Holowaychukbefore adoption6230ec5
lines 307–311why, one step back307 * @param {Function} fn308 * @return {Server} for chaining309 * @api public310 */311Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 312why, one step back312app.param = function(name, fn){Added support for app.param(fn) to define param logicTj Holowaychukbefore adoptionb1a7310
line 313why, one step back313 // arraymore docsTj Holowaychukbefore adoption6230ec5
lines 314–317why, one step back314 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 back318 // param logic319 } else if ('function' == typeof name) {320 this.routes.param(name);321 // singlemore docsTj Holowaychukbefore adoption6230ec5
lines 322–325why, one step back322 } 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 back326more docsTj Holowaychukbefore adoption6230ec5
lines 327–338why, one step back327 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 */338Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 339why, one step back339app.is = function(type, fn){more docsTj Holowaychukbefore adoption6230ec5
lines 340–354why, one step back340 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 */354Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 355why, one step back355app.set = function(setting, val){more docsTj Holowaychukbefore adoption6230ec5
lines 356–375why, one step back356 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 */375Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 376why, one step back376app.enabled = function(setting){more docsTj Holowaychukbefore adoption6230ec5
lines 377–387why, one step back377 return !!this.set(setting);378};379380/**381 * Check if `setting` is disabled.382 *383 * @param {String} setting384 * @return {Boolean}385 * @api public386 */387Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 388why, one step back388app.disabled = function(setting){more docsTj Holowaychukbefore adoption6230ec5
lines 389–399why, one step back389 return !this.set(setting);390};391392/**393 * Enable `setting`.394 *395 * @param {String} setting396 * @return {Server} for chaining397 * @api public398 */399Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 400why, one step back400app.enable = function(setting){more docsTj Holowaychukbefore adoption6230ec5
lines 401–411why, one step back401 return this.set(setting, true);402};403404/**405 * Disable `setting`.406 *407 * @param {String} setting408 * @return {Server} for chaining409 * @api public410 */411Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 412why, one step back412app.disable = function(setting){more docsTj Holowaychukbefore adoption6230ec5
lines 413–424why, one step back413 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 */424Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 425why, one step back425app.redirect = function(key, url){more docsTj Holowaychukbefore adoption6230ec5
lines 426–430why, one step back426 this.redirects[key] = url;427 return this;428};429430/**docsTj Holowaychukbefore adoptionf46ae9f
lines 431–434why, one step back431 * 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 back435 *Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 436–450why, one step back436 * 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 back451 * @param {Function} fn452 * @return {Server} for chaining453 * @api public454 */455Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 456why, one step back456app.configure = function(env, fn){Added support for multiple env app.configure() calls. Closes #735Tj Holowaychukbefore adoption9f2b344
lines 457–461why, one step back457 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 back462 return this;463};464Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 465–467why, one step back465/**466 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.467 */more docsTj Holowaychukbefore adoption6230ec5
line 468why, one step back468misc refactorTj Holowaychukbefore adoption0477a53
line 469why, one step back469methods.forEach(function(method){Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 470why, one step back470 app[method] = function(path){Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 471–472why, one step back471 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 back473 if (!this.__usedRouter) this.use(this.router);Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 474–475why, one step back474 return this.routes._route.apply(this.routes, args);475 }misc refactorTj Holowaychukbefore adoption0477a53
line 476why, one step back476});more docsTj Holowaychukbefore adoption6230ec5
line 477why, one step back477Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 478–486why, one step back478/**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 back487Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
lines 488–499why, one step back488app.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 alias499Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 500why, one step back500app.del = app.delete;Refactored router. Closes #639Tj Holowaychukbefore adoption80e9ffb
line 501why, one step back501