javascript - Extend node application with new actions -


i've created application handle basic actions save(e.g. saving file in file system) edit etc ,now want users have ability extend functionality new actions,for example user clone application , add additional file new actions (and callback) , register on event

i should new actions under hood,my question how can take new actions new file , run in process,simple example helpful

update lets assume file handle action , user want add additional action delete

var fs = require('fs'); module.exports = {     fileaction: function (req, res, filepath) {         var urlaction = urlpath.substr(urlpath.lastindexof("/") + 1);         if (urlaction === 'save') {             this.save(req,res,filepath);         } else             this.delete(req,res,filepath);     },      save: function (req,res,filepath) {         var writestream = fs.createwritestream(filepath, {flags: 'w'});         req.pipe(writestream);         res.writehead(200, { 'content-type': 'text/plain' });     },      delete: function (req,res,filepath) {      },  } 

and delete code should this

 filepath = 'c://test.txt';     fs.unlinksync(filepath); 

now lordvlad suggest user should have new file specific implementation should used lordvlad suggestion design flow,my question how add delete functionality(which simple) , make work,like poc this.

i imagine plugin system so:

main.js

// setup var ee = require('events').eventemitter; var glob = require('glob'); var eventbus = new ee();  // find plugins glob("plugins/*.js", function(err, files) {   if (err) {     // error handling here   }    files.foreach(function(file) {     var plugin = require(file);     plugin(eventbus);   }); }); 

plugins/my-plugin.js

module.exports = function(eventbus) {   // interesting    // listen events on event bus   eventbus.on("foo", function(e){      //    });     eventemitter.emit("pluginready", "my-plugin"); }; 

of course substitute event emitter global object, or make plugin handle callbacks passing callback instead of event bus. think key aspect load plugins (done within glob ... require block) , and make them fit system (which need figure out or provide code samples of have can give hint).

update after ops update

main.js

var glob = require('glob'); var xtend = require('xtend');    module.exports = {   save: function(..){..},   load: function(..){..}, }  // typeof module.exports.delete === 'undefined', // cannot call module.exports delete or similar  glob("plugins/*.js", function(err, files) {   files.foreach(function(file){     var plugin = require(file);      // call plugin initializer if available     if (typeof plugin.init === "function")       plugin.init();      xtend(module.exports, plugin);   });     // here plugins loaded , can used   // i.e. can following   module.exports.delete(...); }); 

plugins/my-plugin.js

module.exports = {   delete: function(..){..},   init: function() {     // when plugin loads   } } 

mind though, plugin overwrite plugin's methods.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -