Skip to content
Myr

removed old http.js

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d0585bd · Browse files · 1 file+0 added−580 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+0 added−580 removed

  • other:+0 added−580 removed

1 file+0 added−580 removed

deleted:lib/http.js-580 removed
12/*!3 * Express - HTTPServer4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var connect = require('connect')13 , router = require('./router')14 , Router = require('./router')15 , view = require('./view')16 , toArray = require('./utils').toArray17 , methods = router.methods.concat('del', 'all')18 , res = require('./response')19 , url = require('url')20 , utils = connect.utils21 , qs = require('qs');2223/**24 * Expose `HTTPServer`.25 */2627exports = module.exports = HTTPServer;2829/**30 * Server proto.31 */3233var app = HTTPServer.prototype;3435/**36 * Initialize a new `HTTPServer` with optional `middleware`.37 *38 * @param {Array} middleware39 * @api public40 */4142function HTTPServer(middleware){43 connect.HTTPServer.call(this, []);44 this.init(middleware);45};4647/**48 * Inherit from `connect.HTTPServer`.49 */5051app.__proto__ = connect.HTTPServer.prototype;5253/**54 * Initialize the server.55 *56 * @param {Array} middleware57 * @api private58 */5960app.init = function(middleware){61 var self = this;62 this.cache = {};63 this.engines = {};64 this.settings = {};65 this.redirects = {};66 this.isCallbacks = {};6768 this.set('home', '/');69 this.set('env', process.env.NODE_ENV || 'development');70 this.use(connect.query());7172 /**73 * Assign several locals with the given `obj`,74 * or return the locals.75 *76 * @param {Object} obj77 * @return {Object|HTTPServer}78 * @api public79 */8081 this.locals = function(obj){82 for (var key in obj) {83 self.locals[key] = obj[key];84 }85 return self;86 };8788 // expose objects to each other89 this.use(function(req, res, next){90 var charset;91 req.query = req.query || {};92 res.setHeader('X-Powered-By', 'Express');93 req.app = res.app = self;94 req.res = res;95 res.req = req;96 req.next = next;9798 // charset99 if (charset = self.set('charset')) res.charset = charset;100101 /**102 * Assign several locals with the given `obj`,103 * or return the locals.104 *105 * @param {Object} obj106 * @return {Object|ServerResponse}107 * @api public108 */109110 res.locals = function(obj){111 for (var key in obj) {112 res.locals[key] = obj[key];113 }114 return res;115 };116117 next();118 });119120 // apply middleware121 if (middleware) middleware.forEach(self.use.bind(self));122123 // router124 this.routes = new Router(this);125 this.__defineGetter__('router', function(){126 this.__usedRouter = true;127 return self.routes.middleware;128 });129130 // default locals131 this.locals.settings = this.settings;132133 // default development configuration134 this.configure('development', function(){135 this.enable('hints');136 });137138 // default production configuration139 this.configure('production', function(){140 this.enable('view cache');141 });142143 // route manipulation methods144 methods.forEach(function(method){145 self.lookup[method] = function(path){146 return self.routes.lookup(method, path);147 };148149 self.match[method] = function(path){150 return self.routes.match(method, path);151 };152153 self.remove[method] = function(path){154 return self.routes.lookup(method, path).remove();155 };156 });157158 // del -> delete159 self.lookup.del = self.lookup.delete;160 self.match.del = self.match.delete;161 self.remove.del = self.remove.delete;162};163164/**165 * Remove routes matching the given `path`.166 *167 * @param {Route} path168 * @return {Boolean}169 * @api public170 */171172app.remove = function(path){173 return this.routes.lookup('all', path).remove();174};175176/**177 * Lookup routes defined with a path178 * equivalent to `path`.179 *180 * @param {Stirng} path181 * @return {Array}182 * @api public183 */184185app.lookup = function(path){186 return this.routes.lookup('all', path);187};188189/**190 * Lookup routes matching the given `url`.191 *192 * @param {Stirng} url193 * @return {Array}194 * @api public195 */196197app.match = function(url){198 return this.routes.match('all', url);199};200201/**202 * Proxy `connect.HTTPServer#use()` to apply settings to203 * mounted applications.204 *205 * @param {String|Function|Server} route206 * @param {Function|Server} middleware207 * @return {Server} for chaining208 * @api public209 */210211app.use = function(route, middleware){212 var app, home, handle;213214 if ('string' != typeof route) {215 middleware = route, route = '/';216 }217218 // express app219 if (middleware.handle && middleware.set) app = middleware;220221 // restore .app property on req and res222 if (app) {223 app.route = route;224 middleware = function(req, res, next) {225 var orig = req.app;226 app.handle(req, res, function(err){227 req.app = res.app = orig;228 next(err);229 });230 };231 }232233 connect.HTTPServer.prototype.use.call(this, route, middleware);234235 // mounted an app, invoke the hook236 // and adjust some settings237 if (app) {238 base = this.set('basepath') || this.route;239 if ('/' == base) base = '';240 base = base + (app.set('basepath') || app.route);241 app.set('basepath', base);242 app.parent = this;243 if (app.__mounted) app.__mounted.call(app, this);244 }245246 return this;247};248249/**250 * Assign a callback `fn` which is called251 * when this `Server` is passed to `Server#use()`.252 *253 * Examples:254 *255 * var app = express.createServer()256 * , blog = express.createServer();257 *258 * blog.mounted(function(parent){259 * // parent is app260 * // "this" is blog261 * });262 *263 * app.use(blog);264 *265 * @param {Function} fn266 * @return {Server} for chaining267 * @api public268 */269270app.mounted = function(fn){271 this.__mounted = fn;272 return this;273};274275/**276 * See: view.register.277 *278 * @return {Server} for chaining279 * @api public280 */281282/**283 * Register the given template engine `exports`284 * as `ext`. For example we may wish to map ".html"285 * files to jade:286 *287 * app.register('.html', require('jade'));288 *289 * or290 *291 * app.register('html', require('jade'));292 *293 * This is also useful for libraries that may not294 * match extensions correctly. For example my haml.js295 * library is installed from npm as "hamljs" so instead296 * of layout.hamljs, we can register the engine as ".haml":297 *298 * app.register('.haml', require('haml-js'));299 *300 * @param {String} ext301 * @param {Object} obj302 * @api public303 */304305app.register = function(ext, exports) {306 if ('.' != ext[0]) ext = '.' + ext;307 this.engines[ext] = exports;308};309310/**311 * Map the given param placeholder `name`(s) to the given callback `fn`.312 * Register the given view helpers `obj`. This method313 * can be called several times to apply additional helpers.314 *315 * @param {Object} obj316 * @return {Server} for chaining317 * @api public318 */319320app.locals = function(obj){321 utils.merge(this._locals, obj);322 return this;323};324325/**326 * Map the given param placeholder `name`(s) to the given callback(s).327 *328 * Param mapping is used to provide pre-conditions to routes329 * which us normalized placeholders. This callback has the same330 * signature as regular middleware, for example below when ":userId"331 * is used this function will be invoked in an attempt to load the user.332 *333 * app.param('userId', function(req, res, next, id){334 * User.find(id, function(err, user){335 * if (err) {336 * next(err);337 * } else if (user) {338 * req.user = user;339 * next();340 * } else {341 * next(new Error('failed to load user'));342 * }343 * });344 * });345 *346 * Passing a single function allows you to map logic347 * to the values passed to `app.param()`, for example348 * this is useful to provide coercion support in a concise manner.349 *350 * The following example maps regular expressions to param values351 * ensuring that they match, otherwise passing control to the next352 * route:353 *354 * app.param(function(name, regexp){355 * if (regexp instanceof RegExp) {356 * return function(req, res, next, val){357 * var captures;358 * if (captures = regexp.exec(String(val))) {359 * req.params[name] = captures;360 * next();361 * } else {362 * next('route');363 * }364 * }365 * }366 * });367 *368 * We can now use it as shown below, where "/commit/:commit" expects369 * that the value for ":commit" is at 5 or more digits. The capture370 * groups are then available as `req.params.commit` as we defined371 * in the function above.372 *373 * app.param('commit', /^\d{5,}$/);374 *375 * For more of this useful functionality take a look376 * at [express-params](http://github.com/visionmedia/express-params).377 *378 * @param {String|Array|Function} name379 * @param {Function} fn380 * @return {Server} for chaining381 * @api public382 */383384app.param = function(name, fn){385 var self = this386 , fns = [].slice.call(arguments, 1);387388 // array389 if (Array.isArray(name)) {390 name.forEach(function(name){391 fns.forEach(function(fn){392 self.param(name, fn);393 });394 });395 // param logic396 } else if ('function' == typeof name) {397 this.routes.param(name);398 // single399 } else {400 if (':' == name[0]) name = name.substr(1);401 fns.forEach(function(fn){402 self.routes.param(name, fn);403 });404 }405406 return this;407};408409/**410 * Register the given callback `fn` for the given `type`.411 *412 * @param {String} type413 * @param {Function} fn414 * @return {Server} for chaining415 * @api public416 */417418app.is = function(type, fn){419 if (!fn) return this.isCallbacks[type];420 this.isCallbacks[type] = fn;421 return this;422};423424/**425 * Assign `setting` to `val`, or return `setting`'s value.426 * Mounted servers inherit their parent server's settings.427 *428 * @param {String} setting429 * @param {String} val430 * @return {Server|Mixed} for chaining, or the setting value431 * @api public432 */433434app.set = function(setting, val){435 if (val === undefined) {436 if (this.settings.hasOwnProperty(setting)) {437 return this.settings[setting];438 } else if (this.parent) {439 return this.parent.set(setting);440 }441 } else {442 this.settings[setting] = val;443 return this;444 }445};446447/**448 * Check if `setting` is enabled.449 *450 * @param {String} setting451 * @return {Boolean}452 * @api public453 */454455app.enabled = function(setting){456 return !!this.set(setting);457};458459/**460 * Check if `setting` is disabled.461 *462 * @param {String} setting463 * @return {Boolean}464 * @api public465 */466467app.disabled = function(setting){468 return !this.set(setting);469};470471/**472 * Enable `setting`.473 *474 * @param {String} setting475 * @return {Server} for chaining476 * @api public477 */478479app.enable = function(setting){480 return this.set(setting, true);481};482483/**484 * Disable `setting`.485 *486 * @param {String} setting487 * @return {Server} for chaining488 * @api public489 */490491app.disable = function(setting){492 return this.set(setting, false);493};494495/**496 * Redirect `key` to `url`.497 *498 * @param {String} key499 * @param {String} url500 * @return {Server} for chaining501 * @api public502 */503504app.redirect = function(key, url){505 this.redirects[key] = url;506 return this;507};508509/**510 * Configure callback for zero or more envs,511 * when no env is specified that callback will512 * be invoked for all environments. Any combination513 * can be used multiple times, in any order desired.514 *515 * Examples:516 *517 * app.configure(function(){518 * // executed for all envs519 * });520 *521 * app.configure('stage', function(){522 * // executed staging env523 * });524 *525 * app.configure('stage', 'production', function(){526 * // executed for stage and production527 * });528 *529 * @param {String} env...530 * @param {Function} fn531 * @return {Server} for chaining532 * @api public533 */534535app.configure = function(env, fn){536 var envs = 'all'537 , args = toArray(arguments);538 fn = args.pop();539 if (args.length) envs = args;540 if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);541 return this;542};543544/**545 * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.546 */547548methods.forEach(function(method){549 app[method] = function(path){550 if (1 == arguments.length) return this.routes.lookup(method, path);551 var args = [method].concat(toArray(arguments));552 if (!this.__usedRouter) this.use(this.router);553 return this.routes._route.apply(this.routes, args);554 }555});556557/**558 * Special-cased "all" method, applying the given route `path`,559 * middleware, and callback to _every_ HTTP method.560 *561 * @param {String} path562 * @param {Function} ...563 * @return {Server} for chaining564 * @api public565 */566567app.all = function(path){568 var args = arguments;569 if (1 == args.length) return this.routes.lookup('all', path);570 methods.forEach(function(method){571 if ('all' == method) return;572 app[method].apply(this, args);573 }, this);574 return this;575};576577// del -> delete alias578579app.del = app.delete;580