Merge branch 'features/https'
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 · parents cd519e9, 54eac36 · Browse files · 6 files·+515 added−465 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+515 added−465 removed
- tests:+2 added−2 removed
- other:+513 added−463 removed
Provenance
- Commit
8261437- Tree
e37866e- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
cd519e9- Parent 2
54eac36
6 files changed+515 added−465 removed
modified:lib/express.js+10 added-5 removed
8 unmodified lines91011121313141516171817 unmodified lines3637383939404041424344454647484449505147525354555614 unmodified lines8 unmodified lines * Module dependencies. */
var connect = require('connect') , Server = require('./server'); , HTTPSServer = require('./https') , HTTPServer = require('./http');
/** * Re-export connect auto-loaders. * 17 unmodified lines * @return {Server} * @api public */
exports.createServer = function(){ return new Server(Array.prototype.slice.call(arguments));exports.createServer = function(options){ if ('object' == typeof options) { return new HTTPSServer(options, Array.prototype.slice.call(arguments, 1)); } else { return new HTTPServer(Array.prototype.slice.call(arguments)); }};
/** * Expose `Server`. * Expose `HTTPServer`. */
exports.Server = Server;exports.HTTPServer = HTTPServer;
/** * View extensions. */14 unmodified linesadded:lib/http.js+463 added
1+2+/*!3+ * Express - HTTPServer4+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5+ * MIT Licensed6+ */7+8+/**9+ * Module dependencies.10+ */11+12+var qs = require('qs')13+ , connect = require('connect')14+ , router = connect.router15+ , methods = router.methods.concat(['del', 'all'])16+ , view = require('./view')17+ , url = require('url')18+ , utils = connect.utils;19+20+/**21+ * Initialize a new `HTTPServer` with optional `middleware`.22+ *23+ * @param {Array} middleware24+ * @api public25+ */26+27+var Server = exports = module.exports = function HTTPServer(middleware){28+ connect.HTTPServer.call(this, []);29+ this.init(middleware);30+};31+32+/**33+ * Inherit from `connect.HTTPServer`.34+ */35+36+Server.prototype.__proto__ = connect.HTTPServer.prototype;37+38+/**39+ * Initialize the server.40+ *41+ * @param {Array} middleware42+ * @api private43+ */44+45+Server.prototype.init = function(middleware){46+ var self = this;47+ this.settings = {};48+ this.redirects = {};49+ this.isCallbacks = {};50+ this.viewHelpers = {};51+ this.dynamicViewHelpers = {};52+ this.errorHandlers = [];53+54+ // Default "home" to / 55+ this.set('home', '/');56+57+ // Set "env" to NODE_ENV, defaulting to "development"58+ this.set('env', process.env.NODE_ENV || 'development');59+60+ // Expose objects to each other61+ this.use(function(req, res, next){62+ req.query = req.query || {};63+ res.headers = { 'X-Powered-By': 'Express' };64+ req.app = res.app = self;65+ req.res = res;66+ res.req = req;67+ req.next = next;68+ // Assign req.params.get69+ if (req.url.indexOf('?') > 0) {70+ var query = url.parse(req.url).query;71+ req.query = qs.parse(query);72+ }73+ next();74+ });75+76+ // Apply middleware77+ if (middleware) {78+ middleware.forEach(function(fn){79+ self.use(fn);80+ });81+ }82+83+ // Use router, expose as app.get(), etc84+ var fn = router(function(app){ self.routes = app; });85+ this.__defineGetter__('router', function(){86+ this.__usedRouter = true;87+ return fn;88+ });89+90+ // Default production configuration91+ this.configure('production', function(){92+ this.enable('cache views');93+ });94+};95+96+/**97+ * Proxy in order to register error handlers.98+ */99+100+Server.prototype.listen = function(){101+ this.registerErrorHandlers();102+ connect.HTTPServer.prototype.listen.apply(this, arguments);103+};104+105+/**106+ * Proxy in order to register error handlers.107+ */108+109+Server.prototype.listenFD = function(){110+ this.registerErrorHandlers();111+ connect.HTTPServer.prototype.listenFD.apply(this, arguments);112+};113+114+/**115+ * When using the vhost() middleware register error handlers.116+ */117+118+Server.prototype.onvhost = function(){119+ this.registerErrorHandlers();120+};121+122+/**123+ * Register error handlers. This is automatically124+ * called from within `Server#listen()` and `Server#listenFD()`.125+ *126+ * @return {Server} for chaining127+ * @api public128+ */129+130+Server.prototype.registerErrorHandlers = function(){131+ this.errorHandlers.forEach(function(fn){132+ this.use(function(err, req, res, next){133+ fn.apply(this, arguments);134+ });135+ }, this);136+ return this;137+};138+139+/**140+ * Proxy `connect.HTTPServer#use()` to apply settings to141+ * mounted applications.142+ *143+ * @param {String|Function|Server} route144+ * @param {Function|Server} middleware145+ * @return {Server} for chaining146+ * @api public147+ */148+149+Server.prototype.use = function(route, middleware){150+ if (typeof route !== 'string') {151+ middleware = route, route = '/';152+ }153+154+ connect.HTTPServer.prototype.use.call(this, route, middleware);155+156+ // Mounted an app157+ if (middleware instanceof Server) {158+ // Home is /:route/:home159+ var app = middleware160+ , home = app.set('home');161+ if (home === '/') home = '';162+ app.set('home', (app.route || '') + home);163+ app.parent = this;164+ // Mounted hook165+ if (app.__mounted) app.__mounted.call(app, this);166+ }167+168+ return this;169+};170+171+/**172+ * Assign a callback `fn` which is called173+ * when this `Server` is passed to `Server#use()`.174+ *175+ * Examples:176+ *177+ * var app = express.createServer(),178+ * blog = express.createServer();179+ *180+ * blog.mounted(function(parent){181+ * // parent is app182+ * // "this" is blog183+ * });184+ *185+ * app.use(blog);186+ *187+ * @param {Function} fn188+ * @return {Server} for chaining189+ * @api public190+ */191+192+Server.prototype.mounted = function(fn){193+ this.__mounted = fn;194+ return this;195+};196+197+/**198+ * See: view.register.199+ *200+ * @return {Server} for chaining201+ * @api public202+ */203+204+Server.prototype.register = function(){205+ view.register.apply(this, arguments);206+ return this;207+};208+209+/**210+ * Register the given view helpers `obj`. This method211+ * can be called several times to apply additional helpers.212+ *213+ * @param {Object} obj214+ * @return {Server} for chaining215+ * @api public216+ */217+218+Server.prototype.helpers = 219+Server.prototype.locals = function(obj){220+ utils.merge(this.viewHelpers, obj);221+ return this;222+};223+224+/**225+ * Register the given dynamic view helpers `obj`. This method226+ * can be called several times to apply additional helpers.227+ *228+ * @param {Object} obj229+ * @return {Server} for chaining230+ * @api public231+ */232+233+Server.prototype.dynamicHelpers = function(obj){234+ utils.merge(this.dynamicViewHelpers, obj);235+ return this;236+};237+238+/**239+ * Map the given param placeholder `name`(s) to the given callback `fn`.240+ *241+ * Param mapping is used to provide pre-conditions to routes242+ * which us normalized placeholders. For example ":user_id" may243+ * attempt to load the user from the database, where as ":num" may 244+ * pass the value through `parseInt(num, 10)`.245+ *246+ * When the callback function accepts only a single argument, the247+ * value of placeholder is passed:248+ *249+ * app.param('page', function(n){ return parseInt(n, 10); });250+ *251+ * After which "/users/:page" would automatically provide us with252+ * an integer for `req.params.page`. If desired we could use the callback 253+ * signature shown below, and immediately `next(new Error('invalid page'))`254+ * when `parseInt` fails.255+ *256+ * Alternatively the callback may accept the request, response, next, and257+ * the value, acting like middlware:258+ *259+ * app.param('userId', function(req, res, next, id){260+ * User.find(id, function(err, user){261+ * if (err) {262+ * next(err);263+ * } else if (user) {264+ * req.user = user;265+ * next();266+ * } else {267+ * next(new Error('failed to load user'));268+ * }269+ * });270+ * });271+ *272+ * Now every time ":userId" is present, the associated user object273+ * will be loaded and assigned before the route handler is invoked.274+ *275+ * @param {String|Array} name276+ * @param {Function} fn277+ * @return {Server} for chaining278+ * @api public279+ */280+281+Server.prototype.param = function(name, fn){282+ if (Array.isArray(name)) {283+ name.forEach(function(name){284+ this.param(name, fn);285+ }, this);286+ } else {287+ if (':' == name[0]) name = name.substr(1);288+ this.routes.param(name, fn);289+ }290+ return this;291+};292+293+/**294+ * Assign a custom exception handler callback `fn`.295+ * These handlers are always _last_ in the middleware stack.296+ *297+ * @param {Function} fn298+ * @return {Server} for chaining299+ * @api public300+ */301+302+Server.prototype.error = function(fn){303+ this.errorHandlers.push(fn);304+ return this;305+};306+307+/**308+ * Register the given callback `fn` for the given `type`.309+ *310+ * @param {String} type311+ * @param {Function} fn312+ * @return {Server} for chaining313+ * @api public314+ */315+316+Server.prototype.is = function(type, fn){317+ if (!fn) return this.isCallbacks[type];318+ this.isCallbacks[type] = fn;319+ return this;320+};321+322+/**323+ * Assign `setting` to `val`, or return `setting`'s value.324+ * Mounted servers inherit their parent server's settings.325+ *326+ * @param {String} setting327+ * @param {String} val328+ * @return {Server|Mixed} for chaining, or the setting value329+ * @api public330+ */331+332+Server.prototype.set = function(setting, val){333+ if (val === undefined) {334+ if (this.settings.hasOwnProperty(setting)) {335+ return this.settings[setting];336+ } else if (this.parent) {337+ return this.parent.set(setting);338+ }339+ } else {340+ this.settings[setting] = val;341+ return this;342+ }343+};344+345+/**346+ * Check if `setting` is enabled.347+ *348+ * @param {String} setting349+ * @return {Boolean}350+ * @api public351+ */352+353+Server.prototype.enabled = function(setting){354+ return !!this.set(setting);355+};356+357+/**358+ * Check if `setting` is disabled.359+ *360+ * @param {String} setting361+ * @return {Boolean}362+ * @api public363+ */364+365+Server.prototype.disabled = function(setting){366+ return !this.set(setting);367+};368+369+/**370+ * Enable `setting`.371+ *372+ * @param {String} setting373+ * @return {Server} for chaining374+ * @api public375+ */376+377+Server.prototype.enable = function(setting){378+ return this.set(setting, true);379+};380+381+/**382+ * Disable `setting`.383+ *384+ * @param {String} setting385+ * @return {Server} for chaining386+ * @api public387+ */388+389+Server.prototype.disable = function(setting){390+ return this.set(setting, false);391+};392+393+/**394+ * Redirect `key` to `url`.395+ *396+ * @param {String} key397+ * @param {String} url398+ * @return {Server} for chaining399+ * @api public400+ */401+402+Server.prototype.redirect = function(key, url){403+ this.redirects[key] = url;404+ return this;405+};406+407+/**408+ * Configure callback for the given `env`.409+ *410+ * @param {String} env411+ * @param {Function} fn412+ * @return {Server} for chaining413+ * @api public414+ */415+416+Server.prototype.configure = function(env, fn){417+ if (typeof env === 'function') {418+ fn = env, env = 'all';419+ }420+ if ('all' == env || env == this.settings.env) {421+ fn.call(this);422+ }423+ return this;424+};425+426+// Generate routing methods427+428+function generateMethod(method){429+ Server.prototype[method] = function(path, fn){430+ var self = this;431+432+ // Ensure router is mounted433+ if (!this.__usedRouter) {434+ this.use(this.router);435+ }436+437+ // Route specific middleware support438+ if (arguments.length > 2) {439+ var args = Array.prototype.slice.call(arguments, 1);440+ fn = args.pop();441+ (function stack(middleware){442+ middleware.forEach(function(fn){443+ if (Array.isArray(fn)) {444+ stack(fn);445+ } else {446+ self[method](path, fn);447+ }448+ });449+ })(args);450+ }451+452+ // Generate the route453+ this.routes[method](path, fn);454+ return this;455+ };456+ return arguments.callee;457+}458+459+methods.forEach(generateMethod);460+461+// Alias delete as "del"462+463+Server.prototype.del = Server.prototype.delete;added:lib/https.js+40 added
12345678910111213141516171819202122232425262728293031323334353637383940
/*! * Express - HTTPSServer * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var connect = require('connect') , HTTPServer = require('./http') , https = require('https');
/** * Initialize a new `HTTPSServer` with the * given `options`, and optional `middleware`. * * @param {Object} options * @param {Array} middleware * @api public */
var Server = exports = module.exports = function HTTPSServer(options, middleware){ connect.HTTPSServer.call(this, options, []); this.init(middleware);};
/** * Inherit from `connect.HTTPSServer`. */
Server.prototype.__proto__ = connect.HTTPSServer.prototype;
// mixin HTTPServer methods
Object.keys(HTTPServer.prototype).forEach(function(method){ Server.prototype[method] = HTTPServer.prototype[method];});deleted:lib/server.js-458 removed
1-2-/*!3- * Express - Server4- * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5- * MIT Licensed6- */7-8-/**9- * Module dependencies.10- */11-12-var queryString = require('querystring')13- , connect = require('connect')14- , router = connect.router15- , methods = router.methods.concat(['del', 'all'])16- , view = require('./view')17- , url = require('url')18- , utils = connect.utils;19-20-/**21- * Initialize a new `Server` with optional `middleware`.22- *23- * @param {Array} middleware24- * @api public25- */26-27-var Server = exports = module.exports = function Server(middleware){28- var self = this;29- this.settings = {};30- this.redirects = {};31- this.isCallbacks = {};32- this.viewHelpers = {};33- this.dynamicViewHelpers = {};34- this.errorHandlers = [];35- connect.HTTPServer.call(this, []);36-37- // Default "home" to / 38- this.set('home', '/');39-40- // Set "env" to NODE_ENV, defaulting to "development"41- this.set('env', process.env.NODE_ENV || 'development');42-43- // Expose objects to each other44- this.use(function(req, res, next){45- req.query = req.query || {};46- res.headers = { 'X-Powered-By': 'Express' };47- req.app = res.app = self;48- req.res = res;49- res.req = req;50- req.next = next;51- // Assign req.params.get52- if (req.url.indexOf('?') > 0) {53- var query = url.parse(req.url).query;54- req.query = exports.parseQueryString(query);55- }56- next();57- });58-59- // Apply middleware60- if (middleware) {61- middleware.forEach(function(fn){62- self.use(fn);63- });64- }65-66- // Use router, expose as app.get(), etc67- var fn = router(function(app){ self.routes = app; });68- this.__defineGetter__('router', function(){69- this.__usedRouter = true;70- return fn;71- });72-73- // Default production configuration74- this.configure('production', function(){75- this.enable('cache views');76- });77-};78-79-/**80- * Inherit from `connect.HTTPServer`.81- */82-83-Server.prototype.__proto__ = connect.HTTPServer.prototype;84-85-/**86- * Support swappable querystring parsers.87- */88-89-exports.parseQueryString = queryString.parse;90-91-/**92- * Proxy in order to register error handlers.93- */94-95-Server.prototype.listen = function(){96- this.registerErrorHandlers();97- connect.HTTPServer.prototype.listen.apply(this, arguments);98-};99-100-/**101- * Proxy in order to register error handlers.102- */103-104-Server.prototype.listenFD = function(){105- this.registerErrorHandlers();106- connect.HTTPServer.prototype.listenFD.apply(this, arguments);107-};108-109-/**110- * When using the vhost() middleware register error handlers.111- */112-113-Server.prototype.onvhost = function(){114- this.registerErrorHandlers();115-};116-117-/**118- * Register error handlers. This is automatically119- * called from within `Server#listen()` and `Server#listenFD()`.120- *121- * @return {Server} for chaining122- * @api public123- */124-125-Server.prototype.registerErrorHandlers = function(){126- this.errorHandlers.forEach(function(fn){127- this.use(function(err, req, res, next){128- fn.apply(this, arguments);129- });130- }, this);131- return this;132-};133-134-/**135- * Proxy `connect.HTTPServer#use()` to apply settings to136- * mounted applications.137- *138- * @param {String|Function|Server} route139- * @param {Function|Server} middleware140- * @return {Server} for chaining141- * @api public142- */143-144-Server.prototype.use = function(route, middleware){145- if (typeof route !== 'string') {146- middleware = route, route = '/';147- }148-149- connect.HTTPServer.prototype.use.call(this, route, middleware);150-151- // Mounted an app152- if (middleware instanceof Server) {153- // Home is /:route/:home154- var app = middleware155- , home = app.set('home');156- if (home === '/') home = '';157- app.set('home', (app.route || '') + home);158- app.parent = this;159- // Mounted hook160- if (app.__mounted) app.__mounted.call(app, this);161- }162-163- return this;164-};165-166-/**167- * Assign a callback `fn` which is called168- * when this `Server` is passed to `Server#use()`.169- *170- * Examples:171- *172- * var app = express.createServer(),173- * blog = express.createServer();174- *175- * blog.mounted(function(parent){176- * // parent is app177- * // "this" is blog178- * });179- *180- * app.use(blog);181- *182- * @param {Function} fn183- * @return {Server} for chaining184- * @api public185- */186-187-Server.prototype.mounted = function(fn){188- this.__mounted = fn;189- return this;190-};191-192-/**193- * See: view.register.194- *195- * @return {Server} for chaining196- * @api public197- */198-199-Server.prototype.register = function(){200- view.register.apply(this, arguments);201- return this;202-};203-204-/**205- * Register the given view helpers `obj`. This method206- * can be called several times to apply additional helpers.207- *208- * @param {Object} obj209- * @return {Server} for chaining210- * @api public211- */212-213-Server.prototype.helpers = 214-Server.prototype.locals = function(obj){215- utils.merge(this.viewHelpers, obj);216- return this;217-};218-219-/**220- * Register the given dynamic view helpers `obj`. This method221- * can be called several times to apply additional helpers.222- *223- * @param {Object} obj224- * @return {Server} for chaining225- * @api public226- */227-228-Server.prototype.dynamicHelpers = function(obj){229- utils.merge(this.dynamicViewHelpers, obj);230- return this;231-};232-233-/**234- * Map the given param placeholder `name`(s) to the given callback `fn`.235- *236- * Param mapping is used to provide pre-conditions to routes237- * which us normalized placeholders. For example ":user_id" may238- * attempt to load the user from the database, where as ":num" may 239- * pass the value through `parseInt(num, 10)`.240- *241- * When the callback function accepts only a single argument, the242- * value of placeholder is passed:243- *244- * app.param('page', function(n){ return parseInt(n, 10); });245- *246- * After which "/users/:page" would automatically provide us with247- * an integer for `req.params.page`. If desired we could use the callback 248- * signature shown below, and immediately `next(new Error('invalid page'))`249- * when `parseInt` fails.250- *251- * Alternatively the callback may accept the request, response, next, and252- * the value, acting like middlware:253- *254- * app.param('userId', function(req, res, next, id){255- * User.find(id, function(err, user){256- * if (err) {257- * next(err);258- * } else if (user) {259- * req.user = user;260- * next();261- * } else {262- * next(new Error('failed to load user'));263- * }264- * });265- * });266- *267- * Now every time ":userId" is present, the associated user object268- * will be loaded and assigned before the route handler is invoked.269- *270- * @param {String|Array} name271- * @param {Function} fn272- * @return {Server} for chaining273- * @api public274- */275-276-Server.prototype.param = function(name, fn){277- if (Array.isArray(name)) {278- name.forEach(function(name){279- this.param(name, fn);280- }, this);281- } else {282- if (':' == name[0]) name = name.substr(1);283- this.routes.param(name, fn);284- }285- return this;286-};287-288-/**289- * Assign a custom exception handler callback `fn`.290- * These handlers are always _last_ in the middleware stack.291- *292- * @param {Function} fn293- * @return {Server} for chaining294- * @api public295- */296-297-Server.prototype.error = function(fn){298- this.errorHandlers.push(fn);299- return this;300-};301-302-/**303- * Register the given callback `fn` for the given `type`.304- *305- * @param {String} type306- * @param {Function} fn307- * @return {Server} for chaining308- * @api public309- */310-311-Server.prototype.is = function(type, fn){312- if (!fn) return this.isCallbacks[type];313- this.isCallbacks[type] = fn;314- return this;315-};316-317-/**318- * Assign `setting` to `val`, or return `setting`'s value.319- * Mounted servers inherit their parent server's settings.320- *321- * @param {String} setting322- * @param {String} val323- * @return {Server|Mixed} for chaining, or the setting value324- * @api public325- */326-327-Server.prototype.set = function(setting, val){328- if (val === undefined) {329- if (this.settings.hasOwnProperty(setting)) {330- return this.settings[setting];331- } else if (this.parent) {332- return this.parent.set(setting);333- }334- } else {335- this.settings[setting] = val;336- return this;337- }338-};339-340-/**341- * Check if `setting` is enabled.342- *343- * @param {String} setting344- * @return {Boolean}345- * @api public346- */347-348-Server.prototype.enabled = function(setting){349- return !!this.set(setting);350-};351-352-/**353- * Check if `setting` is disabled.354- *355- * @param {String} setting356- * @return {Boolean}357- * @api public358- */359-360-Server.prototype.disabled = function(setting){361- return !this.set(setting);362-};363-364-/**365- * Enable `setting`.366- *367- * @param {String} setting368- * @return {Server} for chaining369- * @api public370- */371-372-Server.prototype.enable = function(setting){373- return this.set(setting, true);374-};375-376-/**377- * Disable `setting`.378- *379- * @param {String} setting380- * @return {Server} for chaining381- * @api public382- */383-384-Server.prototype.disable = function(setting){385- return this.set(setting, false);386-};387-388-/**389- * Redirect `key` to `url`.390- *391- * @param {String} key392- * @param {String} url393- * @return {Server} for chaining394- * @api public395- */396-397-Server.prototype.redirect = function(key, url){398- this.redirects[key] = url;399- return this;400-};401-402-/**403- * Configure callback for the given `env`.404- *405- * @param {String} env406- * @param {Function} fn407- * @return {Server} for chaining408- * @api public409- */410-411-Server.prototype.configure = function(env, fn){412- if (typeof env === 'function') {413- fn = env, env = 'all';414- }415- if ('all' == env || env == this.settings.env) {416- fn.call(this);417- }418- return this;419-};420-421-// Generate routing methods422-423-function generateMethod(method){424- Server.prototype[method] = function(path, fn){425- var self = this;426-427- // Ensure router is mounted428- if (!this.__usedRouter) {429- this.use(this.router);430- }431-432- // Route specific middleware support433- if (arguments.length > 2) {434- var args = Array.prototype.slice.call(arguments, 1);435- fn = args.pop();436- (function stack(middleware){437- middleware.forEach(function(fn){438- if (Array.isArray(fn)) {439- stack(fn);440- } else {441- self[method](path, fn);442- }443- });444- })(args);445- }446-447- // Generate the route448- this.routes[method](path, fn);449- return this;450- };451- return arguments.callee;452-}453-454-methods.forEach(generateMethod);455-456-// Alias delete as "del"457-458-Server.prototype.del = Server.prototype.delete;modified:test/express.test.js+1 added-1 removed
9 unmodified lines10 11 module.exports = {12 'test inheritance': function(){13 var server = express.createServer();14- server.should.be.an.instanceof(connect.Server);14+ server.should.be.an.instanceof(connect.HTTPServer);15 },16 17 'test connect middleware autoloaders': function(){18 express.errorHandler.should.equal(connect.errorHandler);507 unmodified linesmodified:test/view.test.js+1 added-1 removed
389 unmodified lines390 session: function(req, res){391 ++calls;392 req.should.be.a('object');393 res.should.be.a('object');394- this.should.be.an.instanceof(express.Server);394+ this.should.equal(app);395 return req.session;396 }397 });398 505 unmodified lines