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
Provenance
- Commit
5f26808- Tree
0092ce3- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
b3ae8bb
3 files changed+135 added−122 removed
modified:examples/chat/app.js+1 added-1 removed
25 unmodified lines2627282930303132333422 unmodified lines25 unmodified lines res.render('chat.html.haml', { locals: { title: 'Chat', messages: messages, name: self.session.name, name: req.session.name, usersOnline: len } }) });22 unmodified linesadded:lib/express/application.js+132 added
1+2+/*!3+ * Express - Application4+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5+ * MIT Licensed6+ */7+8+/**9+ * Module dependencies.10+ */11+12+var sys = require('sys'),13+ connect = require('connect'),14+ rest = require('connect/providers/rest');15+16+/**17+ * Initialize a new Application with optional middleware.18+ *19+ * @param {Array} middleware20+ * @api public21+ */22+23+var 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+}31+32+sys.inherits(Application, connect.Server);33+34+/**35+ * Listen on the given port after running configuration blocks.36+ *37+ * @param {Number} port38+ * @api public39+ */40+41+Application.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+};46+47+/**48+ * Run config callbacks for the given environment(s);49+ *50+ * @param {String} env ...51+ * @return {Application}52+ * @api public53+ */54+55+Application.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+};67+68+69+/**70+ * Assign setting a value71+ *72+ * @param {String} setting73+ * @param {String} val74+ * @return {Application}75+ * @api public76+ */77+78+Application.prototype.set = function(setting, val){79+ this.settings[setting] = val;80+ return this;81+};82+83+/**84+ * Enable setting.85+ *86+ * @param {String} setting87+ * @return {Application}88+ * @api public89+ */90+91+Application.prototype.enable = function(setting){92+ return this.set(setting, true);93+};94+95+/**96+ * Disable setting.97+ *98+ * @param {String} setting99+ * @return {Application}100+ * @api public101+ */102+103+Application.prototype.disable = function(setting){104+ return this.set(setting, false);105+};106+107+/**108+ * Disable setting.109+ *110+ * @param {String} setting111+ * @return {Application}112+ * @api public113+ */114+115+Application.prototype.configure = function(env, fn){116+ if (typeof env === 'function') fn = env, env = 'any';117+ (this.config[env] = this.config[env] || []).push(fn);118+};119+120+// Generate RESTful methods121+122+(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
4 unmodified lines56789101112131415169101112131423242526151617293031323334353618193839204142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139212223246 unmodified lines4 unmodified lines * MIT Licensed */
/** * Module dependencies. */
var sys = require('sys'), connect = require('connect'), rest = require('connect/providers/rest');
/** * Framework version. */
exports.version = '0.14.0';
/** * Initialize a new Application with optional middleware. * * @param {Array} middleware * @api public * Module dependencies. */
function Application(middleware){ this.config = {}; this.settings = {}; this.routes = { get: [], post: [], put: [], del: []}; connect.Server.call(this, (middleware || []).concat([ { provider: 'rest', routes: this.routes } ]));}var Application = require('./application').Application;
sys.inherits(Application, connect.Server);
/** * Listen on the given port after running configuration blocks. * * @param {Number} port * @api public */
Application.prototype.listen = function(port){ this.runConfig('any', 'development'); connect.Server.prototype.listen.call(this, port);};
/** * Run config callbacks for the given environment(s); * * @param {String} env ... * @return {Application} * @api public */
Application.prototype.runConfig = function(){ for (var i = 0, len = arguments.length; i < len; ++i) { var env = arguments[i]; if (env in this.config) { var config = this.config[env]; config.forEach(function(fn){ fn.call(this); }, this); } } return this;};
/** * Assign setting a value * * @param {String} setting * @param {String} val * @return {Application} * @api public */
Application.prototype.set = function(setting, val){ this.settings[setting] = val; return this;};
/** * Enable setting. * * @param {String} setting * @return {Application} * @api public */
Application.prototype.enable = function(setting){ return this.set(setting, true);};
/** * Disable setting. * * @param {String} setting * @return {Application} * @api public */
Application.prototype.disable = function(setting){ return this.set(setting, false);};
/** * Disable setting. * * @param {String} setting * @return {Application} * @api public */
Application.prototype.configure = function(env, fn){ if (typeof env === 'function') fn = env, env = 'any'; (this.config[env] = this.config[env] || []).push(fn);};
// Generate RESTful methods
(function(method){ Application.prototype[method] = function(path, fn){ var routes = {}; routes[method] = {}; routes[method][path] = fn; rest.normalize(routes); this.routes[method] = this.routes[method].concat(routes[method]); return this; }; return arguments.callee;})('get')('post')('put')('del');
/** * Return a new application with optional middleware. * * @param {Array} middleware * @return {Application}6 unmodified lines