Skip to content
Myr

Added application.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 b3ae8bb · Browse files · 3 files+135 added−122 removed
This commit

State

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

Diff stats

3 files+135 added−122 removed

  • other:+135 added−122 removed

3 files+135 added−122 removed

modified:examples/chat/app.js+1 added-1 removed
added:lib/express/application.js+132 added
12/*!3 * Express - Application4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var sys = require('sys'),13 connect = require('connect'),14 rest = require('connect/providers/rest');1516/**17 * Initialize a new Application with optional middleware.18 *19 * @param {Array} middleware20 * @api public21 */2223var Application = exports.Application = function Application(middleware){24 this.config = {};25 this.settings = {};26 this.routes = { get: [], post: [], put: [], del: []};27 connect.Server.call(this, (middleware || []).concat([28 { provider: 'rest', routes: this.routes }29 ]));30}3132sys.inherits(Application, connect.Server);3334/**35 * Listen on the given port after running configuration blocks.36 *37 * @param {Number} port38 * @api public39 */4041Application.prototype.listen = function(port){42 var env = process.env.EXPRESS_ENV || process.connectEnv.name;43 this.runConfig('any', env);44 connect.Server.prototype.listen.call(this, port);45};4647/**48 * Run config callbacks for the given environment(s);49 *50 * @param {String} env ...51 * @return {Application}52 * @api public53 */5455Application.prototype.runConfig = function(){56 for (var i = 0, len = arguments.length; i < len; ++i) {57 var env = arguments[i];58 if (env in this.config) {59 var config = this.config[env];60 config.forEach(function(fn){61 fn.call(this);62 }, this);63 }64 }65 return this;66};676869/**70 * Assign setting a value71 *72 * @param {String} setting73 * @param {String} val74 * @return {Application}75 * @api public76 */7778Application.prototype.set = function(setting, val){79 this.settings[setting] = val;80 return this;81};8283/**84 * Enable setting.85 *86 * @param {String} setting87 * @return {Application}88 * @api public89 */9091Application.prototype.enable = function(setting){92 return this.set(setting, true);93};9495/**96 * Disable setting.97 *98 * @param {String} setting99 * @return {Application}100 * @api public101 */102103Application.prototype.disable = function(setting){104 return this.set(setting, false);105};106107/**108 * Disable setting.109 *110 * @param {String} setting111 * @return {Application}112 * @api public113 */114115Application.prototype.configure = function(env, fn){116 if (typeof env === 'function') fn = env, env = 'any';117 (this.config[env] = this.config[env] || []).push(fn);118};119120// Generate RESTful methods121122(function(method){123 Application.prototype[method] = function(path, fn){124 var routes = {};125 routes[method] = {};126 routes[method][path] = fn;127 rest.normalize(routes);128 this.routes[method] = this.routes[method].concat(routes[method]);129 return this;130 };131 return arguments.callee;132})('get')('post')('put')('del');
modified:lib/express/index.js+2 added-121 removed