Skip to content
Myr

2 spaces in server.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 347aa56 · Browse files · 1 file+126 added−126 removed
This commit

State

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

Diff stats

1 file+126 added−126 removed

  • other:+126 added−126 removed

1 file+126 added−126 removed

modified:lib/express/server.js+126 added-126 removed
7 unmodified lines
8/**9 * Module dependencies.10 */1112var url = require('url'),13 view = require('./view'),14 connect = require('connect'),15 utils = require('connect/utils'),16 queryString = require('querystring'),17 router = require('connect/middleware/router');12var url = require('url')13 , view = require('./view')14 , connect = require('connect')15 , utils = connect.utils16 , queryString = require('querystring')17 , router = require('connect/middleware/router');1819/**20 * Initialize a new `Server` with optional `middleware`.21 *
1 unmodified line
23 * @api public24 */2526var Server = exports = module.exports = function Server(middleware){27 var self = this;28 this.config = {};29 this.settings = {};30 this.redirects = {};31 this.viewHelpers = {};32 this.dynamicViewHelpers = {};33 this.errorHandlers = [];34 connect.Server.call(this, middleware || []);27 var self = this;28 this.config = {};29 this.settings = {};30 this.redirects = {};31 this.viewHelpers = {};32 this.dynamicViewHelpers = {};33 this.errorHandlers = [];34 connect.Server.call(this, middleware || []);3536 // Default "home" to / 37 this.set('home', '/');36 // Default "home" to / 37 this.set('home', '/');3839 // DEPRECATED: remove in 1.040 if (process.env.EXPRESS_ENV) {41 process.env.NODE_ENV = process.env.EXPRESS_ENV;42 console.warn('\x1b[33mWarning\x1b[0m: EXPRESS_ENV is deprecated, use NODE_ENV.');43 }39 // DEPRECATED: remove in 1.040 if (process.env.EXPRESS_ENV) {41 process.env.NODE_ENV = process.env.EXPRESS_ENV;42 console.warn('\x1b[33mWarning\x1b[0m: EXPRESS_ENV is deprecated, use NODE_ENV.');43 }4445 // Set "env" to NODE_ENV, defaulting to "development"46 this.set('env', process.env.NODE_ENV || 'development');45 // Set "env" to NODE_ENV, defaulting to "development"46 this.set('env', process.env.NODE_ENV || 'development');4748 // Expose objects to each other49 this.use(function(req, res, next){50 req.query = {};51 res.headers = {};52 req.app = res.app = self;53 req.res = res;54 res.req = req;55 req.next = next;56 // Assign req.params.get57 if (req.url.indexOf('?') > 0) {58 var query = url.parse(req.url).query;59 req.query = exports.parseQueryString(query);60 }61 next();62 });48 // Expose objects to each other49 this.use(function(req, res, next){50 req.query = {};51 res.headers = {};52 req.app = res.app = self;53 req.res = res;54 res.req = req;55 req.next = next;56 // Assign req.params.get57 if (req.url.indexOf('?') > 0) {58 var query = url.parse(req.url).query;59 req.query = exports.parseQueryString(query);60 }61 next();62 });6364 // Use router, expose as app.get(), etc65 var fn = router(function(app){ self.routes = app; });66 this.__defineGetter__('router', function(){67 this.__usedRouter = true;68 return fn;69 });64 // Use router, expose as app.get(), etc65 var fn = router(function(app){ self.routes = app; });66 this.__defineGetter__('router', function(){67 this.__usedRouter = true;68 return fn;69 });70};7172/**73 * Inherit from `connect.Server`.
11 unmodified lines
85 * Proxy in order to register error handlers.86 */8788Server.prototype.listen = function(){89 this.registerErrorHandlers();90 connect.Server.prototype.listen.apply(this, arguments);89 this.registerErrorHandlers();90 connect.Server.prototype.listen.apply(this, arguments);91};9293/**94 * Proxy in order to register error handlers.95 */9697Server.prototype.listenFD = function(){98 this.registerErrorHandlers();99 connect.Server.prototype.listenFD.apply(this, arguments);98 this.registerErrorHandlers();99 connect.Server.prototype.listenFD.apply(this, arguments);100};101102/**103 * Register error handlers. This is automatically
3 unmodified lines
107 * @api public108 */109110Server.prototype.registerErrorHandlers = function(){111 this.errorHandlers.forEach(function(fn){112 this.use(function(err, req, res, next){113 fn.apply(this, arguments);114 });115 }, this);116 return this;111 this.errorHandlers.forEach(function(fn){112 this.use(function(err, req, res, next){113 fn.apply(this, arguments);114 });115 }, this);116 return this;117};118119/**120 * Proxy `connect.Server#use()` to apply settings to
5 unmodified lines
126 * @api public127 */128129Server.prototype.use = function(route, middleware){130 if (typeof route !== 'string') {131 middleware = route, route = '/';132 }130 if (typeof route !== 'string') {131 middleware = route, route = '/';132 }133134 connect.Server.prototype.use.call(this, route, middleware);134 connect.Server.prototype.use.call(this, route, middleware);135136 // Mounted an app137 if (middleware instanceof Server) {138 // Home is /:route/:home139 var app = middleware,140 home = app.set('home');141 if (home === '/') home = '';142 app.set('home', (app.route || '') + home);143 app.parent = this;144 // Mounted hook145 if (app.__mounted) app.__mounted.call(app, this);146 }136 // Mounted an app137 if (middleware instanceof Server) {138 // Home is /:route/:home139 var app = middleware140 , home = app.set('home');141 if (home === '/') home = '';142 app.set('home', (app.route || '') + home);143 app.parent = this;144 // Mounted hook145 if (app.__mounted) app.__mounted.call(app, this);146 }147148 return this;148 return this;149};150151/**152 * Assign a callback `fn` which is called
16 unmodified lines
169 * @api public170 */171172Server.prototype.mounted = function(fn){173 this.__mounted = fn;174 return this;173 this.__mounted = fn;174 return this;175};176177/**178 * See: view.register.
2 unmodified lines
181 * @api public182 */183184Server.prototype.register = function(){185 view.register.apply(this, arguments);186 return this;185 view.register.apply(this, arguments);186 return this;187};188189/**190 * Register the given view helpers `obj`. This method
4 unmodified lines
195 * @api public196 */197198Server.prototype.helpers = function(obj){199 utils.merge(this.viewHelpers, obj);200 return this;199 utils.merge(this.viewHelpers, obj);200 return this;201};202203/**204 * Register the given dynamic view helpers `obj`. This method
4 unmodified lines
209 * @api public210 */211212Server.prototype.dynamicHelpers = function(obj){213 utils.merge(this.dynamicViewHelpers, obj);214 return this;213 utils.merge(this.dynamicViewHelpers, obj);214 return this;215};216217/**218 * Assign a custom exception handler callback `fn`.
4 unmodified lines
223 * @api public224 */225226Server.prototype.error = function(fn){227 this.errorHandlers.push(fn);228 return this;227 this.errorHandlers.push(fn);228 return this;229};230231/**232 * Assign `setting` to `val`, or return `setting`'s value.
5 unmodified lines
238 * @api public239 */240241Server.prototype.set = function(setting, val){242 if (val === undefined) {243 if (this.settings.hasOwnProperty(setting)) {244 return this.settings[setting];245 } else if (this.parent) {246 return this.parent.set(setting);247 }248 } else {249 this.settings[setting] = val;250 return this;242 if (val === undefined) {243 if (this.settings.hasOwnProperty(setting)) {244 return this.settings[setting];245 } else if (this.parent) {246 return this.parent.set(setting);247 }248 } else {249 this.settings[setting] = val;250 return this;251 }252};253254/**255 * Enable `setting`.
3 unmodified lines
259 * @api public260 */261262Server.prototype.enable = function(setting){263 return this.set(setting, true);263 return this.set(setting, true);264};265266/**267 * Disable `setting`.
3 unmodified lines
271 * @api public272 */273274Server.prototype.disable = function(setting){275 return this.set(setting, false);275 return this.set(setting, false);276};277278/**279 * Redirect `key` to `url`.
4 unmodified lines
284 * @api public285 */286287Server.prototype.redirect = function(key, url){288 this.redirects[key] = url;289 return this;288 this.redirects[key] = url;289 return this;290};291292/**293 * Configure callback for the given `env`.
4 unmodified lines
298 * @api public299 */300301Server.prototype.configure = function(env, fn){302 if (typeof env === 'function') {303 fn = env, env = 'all';304 }305 if (env === 'all' || this.set('env') === env) {306 fn.call(this);307 }308 return this;302 if (typeof env === 'function') {303 fn = env, env = 'all';304 }305 if (env === 'all' || this.set('env') === env) {306 fn.call(this);307 }308 return this;309};310311// Generate routing methods312313(function(method){314 Server.prototype[method] = function(path, fn){315 var self = this;314 Server.prototype[method] = function(path, fn){315 var self = this;316317 // Ensure router is mounted318 if (!this.__usedRouter) {319 this.use(this.router);320 }317 // Ensure router is mounted318 if (!this.__usedRouter) {319 this.use(this.router);320 }321322 // Route specific middleware support323 if (arguments.length > 2) {324 var args = Array.prototype.slice.call(arguments, 1);325 fn = args.pop();326 (function stack(middleware){327 middleware.forEach(function(fn){328 if (Array.isArray(fn)) {329 stack(fn);330 } else {331 self[method](path, fn);332 }333 });334 })(args);335 }322 // Route specific middleware support323 if (arguments.length > 2) {324 var args = Array.prototype.slice.call(arguments, 1);325 fn = args.pop();326 (function stack(middleware){327 middleware.forEach(function(fn){328 if (Array.isArray(fn)) {329 stack(fn);330 } else {331 self[method](path, fn);332 }333 });334 })(args);335 }336337 // Generate the route338 this.routes[method](path, fn);339 return this;340 };341 return arguments.callee;337 // Generate the route338 this.routes[method](path, fn);339 return this;340 };341 return arguments.callee;342})('get')('post')('put')('delete');343344// Alias delete as "del"345346Server.prototype.del = Server.prototype.delete;346Server.prototype.del = Server.prototype.delete;