Added Server#{set,enable,disable}() and friends
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 889f8d9 · Browse files · 4 files·+159 added−87 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+159 added−87 removed
- tests:+59 added−0 removed
- other:+100 added−87 removed
Provenance
- Commit
d803924- Tree
3a974e9- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
889f8d9
4 files changed+159 added−87 removed
modified:lib/express/index.js+8 added
15 unmodified lines16171819202122232425262728293015 unmodified lines */
var Server = require('./server');
/** * Shortcut for `new Server(middleware)`. * * @param {Array} middleware * @return {Server} * @api public */
exports.createServer = function(middleware){ return new Server(middleware);};No newline at end of fileNo newline at end of filemodified:lib/express/server.js+91 added-86 removed
29 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181193435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271285 unmodified lines29 unmodified lines }));};
sys.inherits(Server, connect.Server);// // /**// * Listen on the given port after running configuration blocks.// *// * @param {Number} port// * @api public// */// // Server.prototype.listen = function(port){// var env = process.env.EXPRESS_ENV || process.connectEnv.name;// this.runConfig('any', env);// connect.Server.prototype.listen.call(this, port);// };// // /**// * Run config callbacks for the given environment(s);// *// * @param {String} env ...// * @return {Server}// * @api public// */// // Server.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 {Server}// * @api public// */// // Server.prototype.set = function(setting, val){// this.settings[setting] = val;// return this;// };// // /**// * Enable setting.// *// * @param {String} setting// * @return {Server}// * @api public// */// // Server.prototype.enable = function(setting){// return this.set(setting, true);// };// // /**// * Disable setting.// *// * @param {String} setting// * @return {Server}// * @api public// */// // Server.prototype.disable = function(setting){// return this.set(setting, false);// };// // /**// * Disable setting.// *// * @param {String} setting// * @return {Server}// * @api public// */// // Server.prototype.configure = function(env, fn){// if (typeof env === 'function') fn = env, env = 'any';// (this.config[env] = this.config[env] || []).push(fn);// };//
/** * Listen on the given `port` after running configuration blocks. * * @param {Number} port * @api public */
Server.prototype.listen = function(port){ var env = process.env.EXPRESS_ENV || process.connectEnv.name; this.runConfig('any', env); connect.Server.prototype.listen.call(this, port);};
/** * Run config callbacks for the given environment(s); * * @param {String} env ... * @return {Server} * @api public */
Server.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` to `val`, or return `setting`'s value. * * @param {String} setting * @param {String} val * @return {Server|Mixed} this for chaining, or the setting value * @api public */
Server.prototype.set = function(setting, val){ if (val === undefined) { return this.settings[setting]; } else { this.settings[setting] = val; return this; }};
/** * Enable setting. * * @param {String} setting * @return {Server} * @api public */
Server.prototype.enable = function(setting){ return this.set(setting, true);};
/** * Disable setting. * * @param {String} setting * @return {Server} * @api public */
Server.prototype.disable = function(setting){ return this.set(setting, false);};
/** * Disable setting. * * @param {String} setting * @return {Server} * @api public */
Server.prototype.configure = function(env, fn){ if (typeof env === 'function') fn = env, env = 'any'; (this.config[env] = this.config[env] || []).push(fn); return this;};
// Generate RESTful methods
(function(method){ Server.prototype[method] = function(path, fn){5 unmodified linesmodified:test/express.test.js+59 added
12 unmodified lines13 },14 15 'test basic server': function(assert){16 var server = express.createServer();17+18 server.get('/', function(req, res){19 res.writeHead(200, {});20 res.end('wahoo');21 });22+23+ server.put('/user/:id', function(req, res, params){24+ res.writeHead(200, {});25+ res.end('updated user ' + params.id)26+ });27+28 assert.response(server,29 { url: '/' },30 { body: 'wahoo' });31+ 32+ assert.response(server,33+ { url: '/user/12', method: 'PUT' },34+ { body: 'updated user 12' });35+ },36+ 37+ 'test #configure()': function(assert, beforeExit){38+ var calls = [];39+ var server = express.createServer();40+ process.env.EXPRESS_ENV = 'development';41+ 42+ // Config blocks43+ var ret = server.configure(function(){44+ calls.push('any');45+ });46+ server.configure('development', function(){47+ calls.push('dev');48+ });49+ server.configure('production', function(){50+ calls.push('production');51+ });52+53+ assert.equal(ret, server, 'Test #configure() returns server for chaining');54+55+ assert.response(server,56+ { url: '/' },57+ { body: 'Cannot GET /' });58+59+ beforeExit(function(){60+ assert.eql(['any', 'dev'], calls);61+ });62+ },63+ 64+ 'test #set()': function(assert){65+ var app = express.createServer();66+ var ret = app.set('title', 'My App');67+ assert.equal(app, ret, 'Test #set() returns server for chaining');68+ assert.equal('My App', app.set('title'));69+ },70+ 71+ 'test #enable()': function(assert){72+ var app = express.createServer();73+ var ret = app.enable('some feature');74+ assert.equal(app, ret, 'Test #enable() returns server for chaining');75+ assert.strictEqual(true, app.set('some feature'));76+ },77+ 78+ 'test #disable()': function(assert){79+ var app = express.createServer();80+ var ret = app.disable('some feature');81+ assert.equal(app, ret, 'Test #disable() returns server for chaining');82+ assert.strictEqual(false, app.set('some feature'));83 }84 };modified:Makefile+1 added-1 removed
1 2 test:3- @./support/expresso/bin/expresso -I lib test/*.test.js3+ @CONNECT_ENV=test ./support/expresso/bin/expresso -I lib test/*.test.js4 5 .PHONY: test