Permalink
Browse files

fixed path app, added RegExp patterns.

  • Loading branch information...
jed committed Mar 15, 2010
1 parent 48a3403 commit c347d7ee0c977b4a947c79c831ac4f7fca5c3edf
Showing with 217 additions and 21 deletions.
  1. +62 −5 dist/fab-common.js
  2. +71 −7 dist/fab-node.js
  3. +15 −2 examples/node/app.js
  4. +3 −0 lib/common/RegExp.js
  5. +28 −0 lib/common/method.js
  6. +28 −4 lib/common/path.js
  7. +10 −3 lib/node/listener.js
View
@@ -66,15 +66,66 @@ fab.Function = function( fn ) {
};
}
fab.method = function() {
var
methods = {},
len = arguments.length;
for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}
return function() {
var hit = arguments[ 0 ];
return function() {
var miss = arguments[ 0 ] || fab.status( 405 )();
return function( respond ) {
return function( head ) {
var app = head.method in methods ? hit : miss;
app = app( respond );
if ( app ) app = app( head );
return app;
}
}
}
}
}
fab.Number = function( num ) {
return fab.status( num );
}
fab.path = function() {
var
pattern = arguments[ 0 ],
length = pattern.length;
match = {
String: function( url ) {
var path = url.pathname;
if ( path.indexOf( pattern ) ) return;
url.pathname = path.substr( pattern.length );
return url;
},
RegExp: function( url ) {
var path = url.pathname, matched;
if ( path.search( pattern ) ) return;
matched = path.match( pattern );
url.pathname = path.substr( matched.shift().length );
url.capture = url.capture || [];
url.capture.push.apply( url.capture, matched );
return url;
}
}[ pattern.constructor.name ];
return function() {
var hit = arguments[ 0 ];
@@ -83,10 +134,12 @@ fab.path = function() {
return function( respond ) {
return function( head ) {
var app = miss;
var
app = miss,
url = match( head.url );
if ( !head.url.indexOf( pattern ) ) {
head.url = head.url.substr( length );
if ( url ) {
head.url = url;
app = hit;
}
@@ -100,6 +153,10 @@ fab.path = function() {
}
}
fab.RegExp = function( re ) {
return fab.path( re );
}
fab.status = function( code ) {
return function( fwd ) {
if ( fwd ) { /* TODO: status handler */ }
View
@@ -68,15 +68,66 @@ fab.Function = function( fn ) {
};
}
fab.method = function() {
var
methods = {},
len = arguments.length;
for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}
return function() {
var hit = arguments[ 0 ];
return function() {
var miss = arguments[ 0 ] || fab.status( 405 )();
return function( respond ) {
return function( head ) {
var app = head.method in methods ? hit : miss;
app = app( respond );
if ( app ) app = app( head );
return app;
}
}
}
}
}
fab.Number = function( num ) {
return fab.status( num );
}
fab.path = function() {
var
pattern = arguments[ 0 ],
length = pattern.length;
match = {
String: function( url ) {
var path = url.pathname;
if ( path.indexOf( pattern ) ) return;
url.pathname = path.substr( pattern.length );
return url;
},
RegExp: function( url ) {
var path = url.pathname, matched;
if ( path.search( pattern ) ) return;
matched = path.match( pattern );
url.pathname = path.substr( matched.shift().length );
url.capture = url.capture || [];
url.capture.push.apply( url.capture, matched );
return url;
}
}[ pattern.constructor.name ];
return function() {
var hit = arguments[ 0 ];
@@ -85,10 +136,12 @@ fab.path = function() {
return function( respond ) {
return function( head ) {
var app = miss;
var
app = miss,
url = match( head.url );
if ( !head.url.indexOf( pattern ) ) {
head.url = head.url.substr( length );
if ( url ) {
head.url = url;
app = hit;
}
@@ -102,6 +155,10 @@ fab.path = function() {
}
}
fab.RegExp = function( re ) {
return fab.path( re );
}
fab.status = function( code ) {
return function( fwd ) {
if ( fwd ) { /* TODO: status handler */ }
@@ -124,7 +181,10 @@ fab.String = function() {
}
fab.listener = function() {
var app = arguments[ 0 ];
var
app = arguments[ 0 ],
url = require( "url" );
return function( request ) {
var
@@ -138,10 +198,14 @@ fab.listener = function() {
down = down({
method: request.method,
headers: request.headers,
url: request.url
url: url.parse(
"http://" +
request.headers.host +
request.url
)
});
}
if ( down ) {
request
.addListener( "data", function( body ) {
View
@@ -15,9 +15,22 @@ app = fab
( "/b", "/b path" )
( "/c", "/c", "/c/c path" )
( "/c", "/c", function( back ) {
return function( head ) {
back({ body: JSON.stringify( head.url ) })()
}
})
( "/d", fab.method( "GET", "POST" ), "YEP!" )
( "/e/", /(\w{2})(\w{2})/, function( back ) {
return function( head ) {
back({ body: JSON.stringify( head.url.capture ) })()
}
})
()
http.createServer( app ).listen( 4011 )
http.createServer( app ).listen( 8000 )
require( "repl" ).start( "> " )
View
@@ -0,0 +1,3 @@
function( re ) {
return fab.path( re );
}
View
@@ -0,0 +1,28 @@
function() {
var
methods = {},
len = arguments.length;
for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}
return function() {
var hit = arguments[ 0 ];
return function() {
var miss = arguments[ 0 ] || fab.status( 405 )();
return function( respond ) {
return function( head ) {
var app = head.method in methods ? hit : miss;
app = app( respond );
if ( app ) app = app( head );
return app;
}
}
}
}
}
View
@@ -1,8 +1,30 @@
function() {
var
pattern = arguments[ 0 ],
length = pattern.length;
match = {
String: function( url ) {
var path = url.pathname;
if ( path.indexOf( pattern ) ) return;
url.pathname = path.substr( pattern.length );
return url;
},
RegExp: function( url ) {
var path = url.pathname, matched;
if ( path.search( pattern ) ) return;
matched = path.match( pattern );
url.pathname = path.substr( matched.shift().length );
url.capture = url.capture || [];
url.capture.push.apply( url.capture, matched );
return url;
}
}[ pattern.constructor.name ];
return function() {
var hit = arguments[ 0 ];
@@ -11,10 +33,12 @@ function() {
return function( respond ) {
return function( head ) {
var app = miss;
var
app = miss,
url = match( head.url );
if ( !head.url.indexOf( pattern ) ) {
head.url = head.url.substr( length );
if ( url ) {
head.url = url;
app = hit;
}
View
@@ -1,5 +1,8 @@
function() {
var app = arguments[ 0 ];
var
app = arguments[ 0 ],
url = require( "url" );
return function( request ) {
var
@@ -13,10 +16,14 @@ function() {
down = down({
method: request.method,
headers: request.headers,
url: request.url
url: url.parse(
"http://" +
request.headers.host +
request.url
)
});
}
if ( down ) {
request
.addListener( "data", function( body ) {

0 comments on commit c347d7e

Please sign in to comment.