Permalink
Browse files

refactored http.js

  • Loading branch information...
1 parent 393d38f commit a3678cd7f69c9fe5d066e118137891f14978336d Tj Holowaychuk committed Apr 26, 2011
Showing with 35 additions and 23 deletions.
  1. +35 −23 lib/http.js
View
@@ -18,13 +18,25 @@ var qs = require('qs')
, utils = connect.utils;
/**
+ * Expose `HTTPServer`.
+ */
+
+exports = module.exports = HTTPServer;
+
+/**
+ * Server proto.
+ */
+
+var app = HTTPServer.prototype;
+
+/**
* Initialize a new `HTTPServer` with optional `middleware`.
*
* @param {Array} middleware
* @api public
*/
-var Server = exports = module.exports = function HTTPServer(middleware){
+function HTTPServer(middleware){
connect.HTTPServer.call(this, []);
this.init(middleware);
};
@@ -33,7 +45,7 @@ var Server = exports = module.exports = function HTTPServer(middleware){
* Inherit from `connect.HTTPServer`.
*/
-Server.prototype.__proto__ = connect.HTTPServer.prototype;
+app.__proto__ = connect.HTTPServer.prototype;
/**
* Initialize the server.
@@ -42,7 +54,7 @@ Server.prototype.__proto__ = connect.HTTPServer.prototype;
* @api private
*/
-Server.prototype.init = function(middleware){
+app.init = function(middleware){
var self = this;
this.cache = {};
this.match = {};
@@ -126,7 +138,7 @@ Server.prototype.init = function(middleware){
* When using the vhost() middleware register error handlers.
*/
-Server.prototype.onvhost = function(){
+app.onvhost = function(){
this.registerErrorHandlers();
};
@@ -137,7 +149,7 @@ Server.prototype.onvhost = function(){
* @api public
*/
-Server.prototype.registerErrorHandlers = function(){
+app.registerErrorHandlers = function(){
this.errorHandlers.forEach(function(fn){
this.use(function(err, req, res, next){
fn.apply(this, arguments);
@@ -156,7 +168,7 @@ Server.prototype.registerErrorHandlers = function(){
* @api public
*/
-Server.prototype.use = function(route, middleware){
+app.use = function(route, middleware){
var app, home, handle;
if ('string' != typeof route) {
@@ -214,7 +226,7 @@ Server.prototype.use = function(route, middleware){
* @api public
*/
-Server.prototype.mounted = function(fn){
+app.mounted = function(fn){
this.__mounted = fn;
return this;
};
@@ -226,7 +238,7 @@ Server.prototype.mounted = function(fn){
* @api public
*/
-Server.prototype.register = function(){
+app.register = function(){
view.register.apply(this, arguments);
return this;
};
@@ -240,8 +252,8 @@ Server.prototype.register = function(){
* @api public
*/
-Server.prototype.helpers =
-Server.prototype.locals = function(obj){
+app.helpers =
+app.locals = function(obj){
utils.merge(this._locals, obj);
return this;
};
@@ -255,7 +267,7 @@ Server.prototype.locals = function(obj){
* @api public
*/
-Server.prototype.dynamicHelpers = function(obj){
+app.dynamicHelpers = function(obj){
utils.merge(this.dynamicViewHelpers, obj);
return this;
};
@@ -303,7 +315,7 @@ Server.prototype.dynamicHelpers = function(obj){
* @api public
*/
-Server.prototype.param = function(name, fn){
+app.param = function(name, fn){
if (Array.isArray(name)) {
name.forEach(function(name){
this.param(name, fn);
@@ -324,7 +336,7 @@ Server.prototype.param = function(name, fn){
* @api public
*/
-Server.prototype.error = function(fn){
+app.error = function(fn){
this.errorHandlers.push(fn);
return this;
};
@@ -338,7 +350,7 @@ Server.prototype.error = function(fn){
* @api public
*/
-Server.prototype.is = function(type, fn){
+app.is = function(type, fn){
if (!fn) return this.isCallbacks[type];
this.isCallbacks[type] = fn;
return this;
@@ -354,7 +366,7 @@ Server.prototype.is = function(type, fn){
* @api public
*/
-Server.prototype.set = function(setting, val){
+app.set = function(setting, val){
if (val === undefined) {
if (this.settings.hasOwnProperty(setting)) {
return this.settings[setting];
@@ -375,7 +387,7 @@ Server.prototype.set = function(setting, val){
* @api public
*/
-Server.prototype.enabled = function(setting){
+app.enabled = function(setting){
return !!this.set(setting);
};
@@ -387,7 +399,7 @@ Server.prototype.enabled = function(setting){
* @api public
*/
-Server.prototype.disabled = function(setting){
+app.disabled = function(setting){
return !this.set(setting);
};
@@ -399,7 +411,7 @@ Server.prototype.disabled = function(setting){
* @api public
*/
-Server.prototype.enable = function(setting){
+app.enable = function(setting){
return this.set(setting, true);
};
@@ -411,7 +423,7 @@ Server.prototype.enable = function(setting){
* @api public
*/
-Server.prototype.disable = function(setting){
+app.disable = function(setting){
return this.set(setting, false);
};
@@ -424,7 +436,7 @@ Server.prototype.disable = function(setting){
* @api public
*/
-Server.prototype.redirect = function(key, url){
+app.redirect = function(key, url){
this.redirects[key] = url;
return this;
};
@@ -438,7 +450,7 @@ Server.prototype.redirect = function(key, url){
* @api public
*/
-Server.prototype.configure = function(env, fn){
+app.configure = function(env, fn){
if ('function' == typeof env) {
fn = env, env = 'all';
}
@@ -451,7 +463,7 @@ Server.prototype.configure = function(env, fn){
// Generate routing methods
function generateMethod(method){
- Server.prototype[method] = function(path){
+ app[method] = function(path){
var self = this;
// Lookup
@@ -475,4 +487,4 @@ methods.forEach(generateMethod);
// Alias delete as "del"
-Server.prototype.del = Server.prototype.delete;
+app.del = app.delete;

0 comments on commit a3678cd

Please sign in to comment.