0.5.2 • Published 7 years ago
modular-express v0.5.2
modular-express
Express Application Composition
This package allows the creation of express application modules which define views, static content, and routes. By implementing optional hook functions in the module, an express application can invoke the defined functions across all modules at key points in the initialization. In this way, the often crowded app.js initialization for an express application need only worry about the sequencing of the module hook invocations. Dependencies on other node packages can be maintained together with the code that use them in the module, not as a direct dependency of the main application.
Quick Start
$ npm install modular-express
Exmaples
index.js (Module)
module.exports = {
name: 'modex-publish',
hook_init: function (app) {
// Gather available content
app.use(function (req, res, next) {
res.locals.public = app.config.public;
if (req.user) {
res.locals.content = app.config.content;
}
next();
});
},
hook_route_public: function (app) {
app.use('/', require('./routes/public.js'));
},
hook_route_auth: function (app) {
app.use('/', require('./routes/content.js'));
},
add_views: function () {
return path.join(__dirname, 'views');
},
add_partials: function () {
return path.join(__dirname, 'views/partials');
},
add_statics: function () {
return path.join(__dirname, 'public');
}
};
app.js (Application)
// Hooks
var hooks = require('modular-express')(app);
// Modules
app.addModule(require('modex-auth'));
app.addModule(require('modex-publish'));
app.addModule(require('modex-shiny'));
app.addModule(require('modex-cs-api'));
app.addModule(require('modex-cs-formds'));
...
// Initialize modules
hooks.invoke_hook_init();
...
// Include modules views
hooks.invoke_add_views();
hooks.invoke_add_partials(hbs);
...
// Public module routes
hooks.invoke_hook_route('public');
...
// Authenticated module routes
hooks.invoke_hook_route('auth');