2. Agenda
• A very brief introduction to node.js
• Personal technical skill background
• What is the minimal features a web framework must have?
• From express to plain node and back to express again
• Framework features
• Live demo
• Why do I roll my own node.js framework?
24. Define controllers
// Ex. users.js
module.exports = {
index : function ( req, res ){
//...
},
show : function ( req, res ){
//...
},
new : function ( req, res ){
//...
},
create : function ( req, res ){
//...
}
// ....
};
25. Load controllers into a hash
var controllers = {};
require( 'fs' ).readdir( PATH_TO_CONTROLLERS, function( err, files ){
if( err ) throw err;
files.forEach( function( file ){
if( /.js$/.test( file )){
var controller = require( PATH_TO_CONTROLLERS + file );
Object.keys( controller ).forEach( function ( action ){
var name = file.replace( '.js', '' );
// Ex. controllers[ 'users/index' ] = controller[ action ];
controllers[ name + '/' + action ] = controller[ action ];
});
}
});
});
40. Flow control
var flow = new Flow();
// set global variables
flow.series( function ( next ){
require( './lib/global' )( base_dir, next );
}).
// load logger
series( function ( next ){
require( CORE_DIR + 'logger' );
// this has to go after the `require`
LOG.sys( 'loading core module: logger' );
next();
}).
// load util
series( function ( next ){
LOG.sys( 'loading core module: utils' );
require( CORE_DIR + 'utils' );
next();
}).
// load model
series( function ( next ){
LOG.sys( 'loading core module: model' );
require( CORE_DIR + 'model' )( next );
}).
// load express
series( function ( next ){
60. You will definitely learn a lot in the process even if your framework is
not as popular as rails you should still give it a try. It really helps you
understanding how a framework work so that you can write better app
code.