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 adf7124 · Browse files · 3 files·+586 added−586 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+586 added−586 removed
- other:+586 added−586 removed
Provenance
- Commit
d0585bd- Tree
41f3e63- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
adf7124
3 files changed+586 added−586 removed
added:lib/application.js+585 added
1+2+/*!3+ * Express - proto4+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5+ * MIT Licensed6+ */7+8+/**9+ * Module dependencies.10+ */11+12+var connect = require('connect')13+ , Router = require('./router')14+ , view = require('./view')15+ , toArray = require('./utils').toArray16+ , methods = Router.methods.concat('del', 'all')17+ , res = require('./response')18+ , union = require('./utils').union19+ , url = require('url')20+ , utils = connect.utils21+ , path = require('path')22+ , extname = path.extname23+ , join = path.join24+ , fs = require('fs')25+ , qs = require('qs');26+27+/**28+ * Expose `HTTPServer`.29+ */30+31+var app = exports = module.exports = {};32+33+/**34+ * Initialize the server.35+ *36+ * @param {Array} middleware37+ * @api private38+ */39+40+app.init = function(middleware){41+ var self = this;42+ this.cache = {};43+ this.settings = {};44+ this.redirects = {};45+ this.isCallbacks = {};46+47+ this.set('home', '/');48+ this.set('env', process.env.NODE_ENV || 'development');49+ this.use(connect.query());50+51+ /**52+ * Assign several locals with the given `obj`,53+ * or return the locals.54+ *55+ * @param {Object} obj56+ * @return {Object|HTTPServer}57+ * @api public58+ */59+60+ this.locals = function(obj){61+ for (var key in obj) {62+ self.locals[key] = obj[key];63+ }64+ return self;65+ };66+67+ // expose objects to each other68+ this.use(function(req, res, next){69+ var charset;70+ req.query = req.query || {};71+ res.setHeader('X-Powered-By', 'Express');72+ req.app = res.app = self;73+ req.res = res;74+ res.req = req;75+ req.next = next;76+77+ // charset78+ if (charset = self.set('charset')) res.charset = charset;79+80+ /**81+ * Assign several locals with the given `obj`,82+ * or return the locals.83+ *84+ * @param {Object} obj85+ * @return {Object|ServerResponse}86+ * @api public87+ */88+89+ res.locals = function(obj){90+ for (var key in obj) {91+ res.locals[key] = obj[key];92+ }93+ return res;94+ };95+96+ next();97+ });98+99+ // apply middleware100+ if (middleware) middleware.forEach(self.use.bind(self));101+102+ // router103+ this.routes = new Router(this);104+ this.__defineGetter__('router', function(){105+ this.__usedRouter = true;106+ return self.routes.middleware;107+ });108+109+ // default locals110+ this.locals.settings = this.settings;111+112+ // default development configuration113+ this.configure('development', function(){114+ this.enable('hints');115+ });116+117+ // default production configuration118+ this.configure('production', function(){119+ this.enable('view cache');120+ });121+122+ // route manipulation methods123+ methods.forEach(function(method){124+ self.lookup[method] = function(path){125+ return self.routes.lookup(method, path);126+ };127+128+ self.match[method] = function(path){129+ return self.routes.match(method, path);130+ };131+132+ self.remove[method] = function(path){133+ return self.routes.lookup(method, path).remove();134+ };135+ });136+137+ // del -> delete138+ self.lookup.del = self.lookup.delete;139+ self.match.del = self.match.delete;140+ self.remove.del = self.remove.delete;141+};142+143+/**144+ * Remove routes matching the given `path`.145+ *146+ * @param {Route} path147+ * @return {Boolean}148+ * @api public149+ */150+151+app.remove = function(path){152+ return this.routes.lookup('all', path).remove();153+};154+155+/**156+ * Lookup routes defined with a path157+ * equivalent to `path`.158+ *159+ * @param {Stirng} path160+ * @return {Array}161+ * @api public162+ */163+164+app.lookup = function(path){165+ return this.routes.lookup('all', path);166+};167+168+/**169+ * Lookup routes matching the given `url`.170+ *171+ * @param {Stirng} url172+ * @return {Array}173+ * @api public174+ */175+176+app.match = function(url){177+ return this.routes.match('all', url);178+};179+180+/**181+ * Proxy `connect.HTTPServer#use()` to apply settings to182+ * mounted applications.183+ *184+ * @param {String|Function|Server} route185+ * @param {Function|Server} middleware186+ * @return {Server} for chaining187+ * @api public188+ */189+190+app.use = function(route, middleware){191+ var app, home, handle;192+193+ if ('string' != typeof route) {194+ middleware = route, route = '/';195+ }196+197+ // express app198+ if (middleware.handle && middleware.set) app = middleware;199+200+ // restore .app property on req and res201+ if (app) {202+ app.route = route;203+ middleware = function(req, res, next) {204+ var orig = req.app;205+ app.handle(req, res, function(err){206+ req.app = res.app = orig;207+ next(err);208+ });209+ };210+ }211+212+ connect.proto.use.call(this, route, middleware);213+214+ // mounted an app, invoke the hook215+ // and adjust some settings216+ if (app) {217+ base = this.set('basepath') || this.route;218+ if ('/' == base) base = '';219+ base = base + (app.set('basepath') || app.route);220+ app.set('basepath', base);221+ app.parent = this;222+ if (app.__mounted) app.__mounted.call(app, this);223+ }224+225+ return this;226+};227+228+/**229+ * Assign a callback `fn` which is called230+ * when this `Server` is passed to `Server#use()`.231+ *232+ * Examples:233+ *234+ * var app = express.createServer()235+ * , blog = express.createServer();236+ *237+ * blog.mounted(function(parent){238+ * // parent is app239+ * // "this" is blog240+ * });241+ *242+ * app.use(blog);243+ *244+ * @param {Function} fn245+ * @return {Server} for chaining246+ * @api public247+ */248+249+app.mounted = function(fn){250+ this.__mounted = fn;251+ return this;252+};253+254+/**255+ * See: view.register.256+ *257+ * @return {Server} for chaining258+ * @api public259+ */260+261+app.register = function(){262+ view.register.apply(this, arguments);263+ return this;264+};265+266+/**267+ * Map the given param placeholder `name`(s) to the given callback `fn`.268+ * Register the given view helpers `obj`. This method269+ * can be called several times to apply additional helpers.270+ *271+ * @param {Object} obj272+ * @return {Server} for chaining273+ * @api public274+ */275+276+app.locals = function(obj){277+ utils.merge(this._locals, obj);278+ return this;279+};280+281+/**282+ * Map the given param placeholder `name`(s) to the given callback(s).283+ *284+ * Param mapping is used to provide pre-conditions to routes285+ * which us normalized placeholders. This callback has the same286+ * signature as regular middleware, for example below when ":userId"287+ * is used this function will be invoked in an attempt to load the user.288+ *289+ * app.param('userId', function(req, res, next, id){290+ * User.find(id, function(err, user){291+ * if (err) {292+ * next(err);293+ * } else if (user) {294+ * req.user = user;295+ * next();296+ * } else {297+ * next(new Error('failed to load user'));298+ * }299+ * });300+ * });301+ *302+ * Passing a single function allows you to map logic303+ * to the values passed to `app.param()`, for example304+ * this is useful to provide coercion support in a concise manner.305+ *306+ * The following example maps regular expressions to param values307+ * ensuring that they match, otherwise passing control to the next308+ * route:309+ *310+ * app.param(function(name, regexp){311+ * if (regexp instanceof RegExp) {312+ * return function(req, res, next, val){313+ * var captures;314+ * if (captures = regexp.exec(String(val))) {315+ * req.params[name] = captures;316+ * next();317+ * } else {318+ * next('route');319+ * }320+ * }321+ * }322+ * });323+ *324+ * We can now use it as shown below, where "/commit/:commit" expects325+ * that the value for ":commit" is at 5 or more digits. The capture326+ * groups are then available as `req.params.commit` as we defined327+ * in the function above.328+ *329+ * app.param('commit', /^\d{5,}$/);330+ *331+ * For more of this useful functionality take a look332+ * at [express-params](http://github.com/visionmedia/express-params).333+ *334+ * @param {String|Array|Function} name335+ * @param {Function} fn336+ * @return {Server} for chaining337+ * @api public338+ */339+340+app.param = function(name, fn){341+ var self = this342+ , fns = [].slice.call(arguments, 1);343+344+ // array345+ if (Array.isArray(name)) {346+ name.forEach(function(name){347+ fns.forEach(function(fn){348+ self.param(name, fn);349+ });350+ });351+ // param logic352+ } else if ('function' == typeof name) {353+ this.routes.param(name);354+ // single355+ } else {356+ if (':' == name[0]) name = name.substr(1);357+ fns.forEach(function(fn){358+ self.routes.param(name, fn);359+ });360+ }361+362+ return this;363+};364+365+/**366+ * Register the given callback `fn` for the given `type`.367+ *368+ * @param {String} type369+ * @param {Function} fn370+ * @return {Server} for chaining371+ * @api public372+ */373+374+app.is = function(type, fn){375+ if (!fn) return this.isCallbacks[type];376+ this.isCallbacks[type] = fn;377+ return this;378+};379+380+/**381+ * Assign `setting` to `val`, or return `setting`'s value.382+ * Mounted servers inherit their parent server's settings.383+ *384+ * @param {String} setting385+ * @param {String} val386+ * @return {Server|Mixed} for chaining, or the setting value387+ * @api public388+ */389+390+app.set = function(setting, val){391+ if (val === undefined) {392+ if (this.settings.hasOwnProperty(setting)) {393+ return this.settings[setting];394+ } else if (this.parent) {395+ return this.parent.set(setting);396+ }397+ } else {398+ this.settings[setting] = val;399+ return this;400+ }401+};402+403+/**404+ * Check if `setting` is enabled.405+ *406+ * @param {String} setting407+ * @return {Boolean}408+ * @api public409+ */410+411+app.enabled = function(setting){412+ return !!this.set(setting);413+};414+415+/**416+ * Check if `setting` is disabled.417+ *418+ * @param {String} setting419+ * @return {Boolean}420+ * @api public421+ */422+423+app.disabled = function(setting){424+ return !this.set(setting);425+};426+427+/**428+ * Enable `setting`.429+ *430+ * @param {String} setting431+ * @return {Server} for chaining432+ * @api public433+ */434+435+app.enable = function(setting){436+ return this.set(setting, true);437+};438+439+/**440+ * Disable `setting`.441+ *442+ * @param {String} setting443+ * @return {Server} for chaining444+ * @api public445+ */446+447+app.disable = function(setting){448+ return this.set(setting, false);449+};450+451+/**452+ * Redirect `key` to `url`.453+ *454+ * @param {String} key455+ * @param {String} url456+ * @return {Server} for chaining457+ * @api public458+ */459+460+app.redirect = function(key, url){461+ this.redirects[key] = url;462+ return this;463+};464+465+/**466+ * Configure callback for zero or more envs,467+ * when no env is specified that callback will468+ * be invoked for all environments. Any combination469+ * can be used multiple times, in any order desired.470+ *471+ * Examples:472+ *473+ * app.configure(function(){474+ * // executed for all envs475+ * });476+ *477+ * app.configure('stage', function(){478+ * // executed staging env479+ * });480+ *481+ * app.configure('stage', 'production', function(){482+ * // executed for stage and production483+ * });484+ *485+ * @param {String} env...486+ * @param {Function} fn487+ * @return {Server} for chaining488+ * @api public489+ */490+491+app.configure = function(env, fn){492+ var envs = 'all'493+ , args = toArray(arguments);494+ fn = args.pop();495+ if (args.length) envs = args;496+ if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);497+ return this;498+};499+500+/**501+ * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.502+ */503+504+methods.forEach(function(method){505+ app[method] = function(path){506+ if (1 == arguments.length) return this.routes.lookup(method, path);507+ var args = [method].concat(toArray(arguments));508+ if (!this.__usedRouter) this.use(this.router);509+ return this.routes._route.apply(this.routes, args);510+ }511+});512+513+/**514+ * Special-cased "all" method, applying the given route `path`,515+ * middleware, and callback to _every_ HTTP method.516+ *517+ * @param {String} path518+ * @param {Function} ...519+ * @return {Server} for chaining520+ * @api public521+ */522+523+app.all = function(path){524+ var args = arguments;525+ if (1 == args.length) return this.routes.lookup('all', path);526+ methods.forEach(function(method){527+ if ('all' == method) return;528+ app[method].apply(this, args);529+ }, this);530+ return this;531+};532+533+// del -> delete alias534+535+app.del = app.delete;536+537+/**538+ * Render the given `view` name with `opts`539+ * and a callback accepting an error and the540+ * rendered template string.541+ *542+ * @param {String} view543+ * @param {String|Function} opts or fn544+ * @param {Function} fn545+ * @api public546+ */547+548+app.render = function(view, opts, fn){549+ var self = this550+ , options = {}551+ , cache = this.cache552+ , engines = this.engines553+ , root = this.set('views') || process.cwd() + '/views';554+555+ // support callback function as second arg556+ if ('function' == typeof opts) {557+ fn = opts, opts = null;558+ }559+560+ // merge app.locals561+ union(options, this.locals);562+563+ // merge render() options564+ if (opts) utils.merge(options, opts);565+566+ // join "view engine" if necessary567+ var ext = extname(view);568+ if (!ext) view += '.' + (ext = this.set('view engine'));569+570+ // pass .cache to the engine571+ options.cache = this.enabled('view cache');572+573+ // when no extension nor "view engine" is given574+ if (!ext) return fn(new Error('failed to find view "' + view + '"'));575+576+ // render577+ try {578+ var engine = cache[ext] = cache[ext] || require(ext);579+ options.filename = join(root, view);580+ view = fs.readFileSync(options.filename, 'utf8');581+ engine.render(view, options, fn);582+ } catch (err) {583+ fn(err);584+ }585+};modified:lib/express.js+1 added-1 removed
8 unmodified lines910111213131415161763 unmodified lines8 unmodified lines * Module dependencies. */
var connect = require('connect') , proto = require('./proto') , proto = require('./application') , Route = require('./router/route') , utils = connect.utils;
/**63 unmodified linesdeleted:lib/proto.js-585 removed
1-2-/*!3- * Express - proto4- * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5- * MIT Licensed6- */7-8-/**9- * Module dependencies.10- */11-12-var connect = require('connect')13- , Router = require('./router')14- , view = require('./view')15- , toArray = require('./utils').toArray16- , methods = Router.methods.concat('del', 'all')17- , res = require('./response')18- , union = require('./utils').union19- , url = require('url')20- , utils = connect.utils21- , path = require('path')22- , extname = path.extname23- , join = path.join24- , fs = require('fs')25- , qs = require('qs');26-27-/**28- * Expose `HTTPServer`.29- */30-31-var app = exports = module.exports = {};32-33-/**34- * Initialize the server.35- *36- * @param {Array} middleware37- * @api private38- */39-40-app.init = function(middleware){41- var self = this;42- this.cache = {};43- this.settings = {};44- this.redirects = {};45- this.isCallbacks = {};46-47- this.set('home', '/');48- this.set('env', process.env.NODE_ENV || 'development');49- this.use(connect.query());50-51- /**52- * Assign several locals with the given `obj`,53- * or return the locals.54- *55- * @param {Object} obj56- * @return {Object|HTTPServer}57- * @api public58- */59-60- this.locals = function(obj){61- for (var key in obj) {62- self.locals[key] = obj[key];63- }64- return self;65- };66-67- // expose objects to each other68- this.use(function(req, res, next){69- var charset;70- req.query = req.query || {};71- res.setHeader('X-Powered-By', 'Express');72- req.app = res.app = self;73- req.res = res;74- res.req = req;75- req.next = next;76-77- // charset78- if (charset = self.set('charset')) res.charset = charset;79-80- /**81- * Assign several locals with the given `obj`,82- * or return the locals.83- *84- * @param {Object} obj85- * @return {Object|ServerResponse}86- * @api public87- */88-89- res.locals = function(obj){90- for (var key in obj) {91- res.locals[key] = obj[key];92- }93- return res;94- };95-96- next();97- });98-99- // apply middleware100- if (middleware) middleware.forEach(self.use.bind(self));101-102- // router103- this.routes = new Router(this);104- this.__defineGetter__('router', function(){105- this.__usedRouter = true;106- return self.routes.middleware;107- });108-109- // default locals110- this.locals.settings = this.settings;111-112- // default development configuration113- this.configure('development', function(){114- this.enable('hints');115- });116-117- // default production configuration118- this.configure('production', function(){119- this.enable('view cache');120- });121-122- // route manipulation methods123- methods.forEach(function(method){124- self.lookup[method] = function(path){125- return self.routes.lookup(method, path);126- };127-128- self.match[method] = function(path){129- return self.routes.match(method, path);130- };131-132- self.remove[method] = function(path){133- return self.routes.lookup(method, path).remove();134- };135- });136-137- // del -> delete138- self.lookup.del = self.lookup.delete;139- self.match.del = self.match.delete;140- self.remove.del = self.remove.delete;141-};142-143-/**144- * Remove routes matching the given `path`.145- *146- * @param {Route} path147- * @return {Boolean}148- * @api public149- */150-151-app.remove = function(path){152- return this.routes.lookup('all', path).remove();153-};154-155-/**156- * Lookup routes defined with a path157- * equivalent to `path`.158- *159- * @param {Stirng} path160- * @return {Array}161- * @api public162- */163-164-app.lookup = function(path){165- return this.routes.lookup('all', path);166-};167-168-/**169- * Lookup routes matching the given `url`.170- *171- * @param {Stirng} url172- * @return {Array}173- * @api public174- */175-176-app.match = function(url){177- return this.routes.match('all', url);178-};179-180-/**181- * Proxy `connect.HTTPServer#use()` to apply settings to182- * mounted applications.183- *184- * @param {String|Function|Server} route185- * @param {Function|Server} middleware186- * @return {Server} for chaining187- * @api public188- */189-190-app.use = function(route, middleware){191- var app, home, handle;192-193- if ('string' != typeof route) {194- middleware = route, route = '/';195- }196-197- // express app198- if (middleware.handle && middleware.set) app = middleware;199-200- // restore .app property on req and res201- if (app) {202- app.route = route;203- middleware = function(req, res, next) {204- var orig = req.app;205- app.handle(req, res, function(err){206- req.app = res.app = orig;207- next(err);208- });209- };210- }211-212- connect.proto.use.call(this, route, middleware);213-214- // mounted an app, invoke the hook215- // and adjust some settings216- if (app) {217- base = this.set('basepath') || this.route;218- if ('/' == base) base = '';219- base = base + (app.set('basepath') || app.route);220- app.set('basepath', base);221- app.parent = this;222- if (app.__mounted) app.__mounted.call(app, this);223- }224-225- return this;226-};227-228-/**229- * Assign a callback `fn` which is called230- * when this `Server` is passed to `Server#use()`.231- *232- * Examples:233- *234- * var app = express.createServer()235- * , blog = express.createServer();236- *237- * blog.mounted(function(parent){238- * // parent is app239- * // "this" is blog240- * });241- *242- * app.use(blog);243- *244- * @param {Function} fn245- * @return {Server} for chaining246- * @api public247- */248-249-app.mounted = function(fn){250- this.__mounted = fn;251- return this;252-};253-254-/**255- * See: view.register.256- *257- * @return {Server} for chaining258- * @api public259- */260-261-app.register = function(){262- view.register.apply(this, arguments);263- return this;264-};265-266-/**267- * Map the given param placeholder `name`(s) to the given callback `fn`.268- * Register the given view helpers `obj`. This method269- * can be called several times to apply additional helpers.270- *271- * @param {Object} obj272- * @return {Server} for chaining273- * @api public274- */275-276-app.locals = function(obj){277- utils.merge(this._locals, obj);278- return this;279-};280-281-/**282- * Map the given param placeholder `name`(s) to the given callback(s).283- *284- * Param mapping is used to provide pre-conditions to routes285- * which us normalized placeholders. This callback has the same286- * signature as regular middleware, for example below when ":userId"287- * is used this function will be invoked in an attempt to load the user.288- *289- * app.param('userId', function(req, res, next, id){290- * User.find(id, function(err, user){291- * if (err) {292- * next(err);293- * } else if (user) {294- * req.user = user;295- * next();296- * } else {297- * next(new Error('failed to load user'));298- * }299- * });300- * });301- *302- * Passing a single function allows you to map logic303- * to the values passed to `app.param()`, for example304- * this is useful to provide coercion support in a concise manner.305- *306- * The following example maps regular expressions to param values307- * ensuring that they match, otherwise passing control to the next308- * route:309- *310- * app.param(function(name, regexp){311- * if (regexp instanceof RegExp) {312- * return function(req, res, next, val){313- * var captures;314- * if (captures = regexp.exec(String(val))) {315- * req.params[name] = captures;316- * next();317- * } else {318- * next('route');319- * }320- * }321- * }322- * });323- *324- * We can now use it as shown below, where "/commit/:commit" expects325- * that the value for ":commit" is at 5 or more digits. The capture326- * groups are then available as `req.params.commit` as we defined327- * in the function above.328- *329- * app.param('commit', /^\d{5,}$/);330- *331- * For more of this useful functionality take a look332- * at [express-params](http://github.com/visionmedia/express-params).333- *334- * @param {String|Array|Function} name335- * @param {Function} fn336- * @return {Server} for chaining337- * @api public338- */339-340-app.param = function(name, fn){341- var self = this342- , fns = [].slice.call(arguments, 1);343-344- // array345- if (Array.isArray(name)) {346- name.forEach(function(name){347- fns.forEach(function(fn){348- self.param(name, fn);349- });350- });351- // param logic352- } else if ('function' == typeof name) {353- this.routes.param(name);354- // single355- } else {356- if (':' == name[0]) name = name.substr(1);357- fns.forEach(function(fn){358- self.routes.param(name, fn);359- });360- }361-362- return this;363-};364-365-/**366- * Register the given callback `fn` for the given `type`.367- *368- * @param {String} type369- * @param {Function} fn370- * @return {Server} for chaining371- * @api public372- */373-374-app.is = function(type, fn){375- if (!fn) return this.isCallbacks[type];376- this.isCallbacks[type] = fn;377- return this;378-};379-380-/**381- * Assign `setting` to `val`, or return `setting`'s value.382- * Mounted servers inherit their parent server's settings.383- *384- * @param {String} setting385- * @param {String} val386- * @return {Server|Mixed} for chaining, or the setting value387- * @api public388- */389-390-app.set = function(setting, val){391- if (val === undefined) {392- if (this.settings.hasOwnProperty(setting)) {393- return this.settings[setting];394- } else if (this.parent) {395- return this.parent.set(setting);396- }397- } else {398- this.settings[setting] = val;399- return this;400- }401-};402-403-/**404- * Check if `setting` is enabled.405- *406- * @param {String} setting407- * @return {Boolean}408- * @api public409- */410-411-app.enabled = function(setting){412- return !!this.set(setting);413-};414-415-/**416- * Check if `setting` is disabled.417- *418- * @param {String} setting419- * @return {Boolean}420- * @api public421- */422-423-app.disabled = function(setting){424- return !this.set(setting);425-};426-427-/**428- * Enable `setting`.429- *430- * @param {String} setting431- * @return {Server} for chaining432- * @api public433- */434-435-app.enable = function(setting){436- return this.set(setting, true);437-};438-439-/**440- * Disable `setting`.441- *442- * @param {String} setting443- * @return {Server} for chaining444- * @api public445- */446-447-app.disable = function(setting){448- return this.set(setting, false);449-};450-451-/**452- * Redirect `key` to `url`.453- *454- * @param {String} key455- * @param {String} url456- * @return {Server} for chaining457- * @api public458- */459-460-app.redirect = function(key, url){461- this.redirects[key] = url;462- return this;463-};464-465-/**466- * Configure callback for zero or more envs,467- * when no env is specified that callback will468- * be invoked for all environments. Any combination469- * can be used multiple times, in any order desired.470- *471- * Examples:472- *473- * app.configure(function(){474- * // executed for all envs475- * });476- *477- * app.configure('stage', function(){478- * // executed staging env479- * });480- *481- * app.configure('stage', 'production', function(){482- * // executed for stage and production483- * });484- *485- * @param {String} env...486- * @param {Function} fn487- * @return {Server} for chaining488- * @api public489- */490-491-app.configure = function(env, fn){492- var envs = 'all'493- , args = toArray(arguments);494- fn = args.pop();495- if (args.length) envs = args;496- if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);497- return this;498-};499-500-/**501- * Delegate `.VERB(...)` calls to `.route(VERB, ...)`.502- */503-504-methods.forEach(function(method){505- app[method] = function(path){506- if (1 == arguments.length) return this.routes.lookup(method, path);507- var args = [method].concat(toArray(arguments));508- if (!this.__usedRouter) this.use(this.router);509- return this.routes._route.apply(this.routes, args);510- }511-});512-513-/**514- * Special-cased "all" method, applying the given route `path`,515- * middleware, and callback to _every_ HTTP method.516- *517- * @param {String} path518- * @param {Function} ...519- * @return {Server} for chaining520- * @api public521- */522-523-app.all = function(path){524- var args = arguments;525- if (1 == args.length) return this.routes.lookup('all', path);526- methods.forEach(function(method){527- if ('all' == method) return;528- app[method].apply(this, args);529- }, this);530- return this;531-};532-533-// del -> delete alias534-535-app.del = app.delete;536-537-/**538- * Render the given `view` name with `opts`539- * and a callback accepting an error and the540- * rendered template string.541- *542- * @param {String} view543- * @param {String|Function} opts or fn544- * @param {Function} fn545- * @api public546- */547-548-app.render = function(view, opts, fn){549- var self = this550- , options = {}551- , cache = this.cache552- , engines = this.engines553- , root = this.set('views') || process.cwd() + '/views';554-555- // support callback function as second arg556- if ('function' == typeof opts) {557- fn = opts, opts = null;558- }559-560- // merge app.locals561- union(options, this.locals);562-563- // merge render() options564- if (opts) utils.merge(options, opts);565-566- // join "view engine" if necessary567- var ext = extname(view);568- if (!ext) view += '.' + (ext = this.set('view engine'));569-570- // pass .cache to the engine571- options.cache = this.enabled('view cache');572-573- // when no extension nor "view engine" is given574- if (!ext) return fn(new Error('failed to find view "' + view + '"'));575-576- // render577- try {578- var engine = cache[ext] = cache[ext] || require(ext);579- options.filename = join(root, view);580- view = fs.readFileSync(options.filename, 'utf8');581- engine.render(view, options, fn);582- } catch (err) {583- fn(err);584- }585-};