Last active
December 19, 2015 23:08
-
-
Save TorbenKoehn/6031968 to your computer and use it in GitHub Desktop.
A simple module system for express (Node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.bind = function( entryPoint, middleware, module ) { | |
if( !module ) { | |
module = middleware; | |
middleware = null; | |
} | |
var methods = { get: 'get', post: 'post', put: 'put', delete: 'del' }; | |
for( var i in module ) { | |
var action = module[ i ]; | |
var key = i; | |
var currentMethod = 'get'; | |
for( var j in methods ) { | |
var method = methods[ j ]; | |
if( i.substring( 0, j.length ) === j ) { | |
currentMethod = method; | |
key = i.charAt( j.length ).toLowerCase() + i.substring( j.length + 1 ) | |
} | |
} | |
var route = path.join( entryPoint, key ) | |
if( path.sep !== '/' ) route = route.split( path.sep ).join( '/' ); //Windows fix | |
var routeWithFormat = route + '.:format'; | |
console.log( 'Binding', currentMethod.toUpperCase(), '->', route, 'to', 'Module->' + key ); | |
if( middleware ) { | |
this[ currentMethod ]( route, middleware, action ); | |
this[ currentMethod ]( routeWithFormat, middleware, action ); | |
if( key === 'index' ) | |
this[ currentMethod ]( entryPoint, middleware, action ); | |
} else { | |
this[ currentMethod ]( route, action ); | |
this[ currentMethod ]( routeWithFormat, action ); | |
if( key === 'index' ) | |
this[ currentMethod ]( entryPoint, action ); | |
} | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment