Plugin system for Node.JS

I am currently working on a large application in Node.JS , on top of Express .

I was hoping to do something plug & play-able, so I started writing my own small plug-in system, the file structure of which is as follows:

root/ | plugins.json # configures all plugins | plugins # contains all plugins | | MyPlugin # contains MyPlugin | | | MyPlugin.js # defines Application hooks for MyPlugin | | | ... 

MyPlugin/ , of course, also contains Routes, Controllers, Views, certain resources, etc.

The standard hooks that the app will call the MyPlugin instance are:

 moduleInit(app) # before starting the server or module has been loaded (if server is already running at module load) moduleStart(app) # right after server has been started or not at all moduleCleanup(app) # right before the party ends 

So far, it’s so good, but now everything is getting complicated (and they will always only get more complicated), since I have to consider the sequence of module initialization (since they can be attached to the processor stack using use , get , post , etc.) and much more another ...

Since I'm still a little new to Node and Express, I have doubts: Maybe there is already a good engine that does all this? I could not find him, which makes me believe that this might be a bad idea? Maybe there is more "node" or "express" - a way to do what I'm missing?

Thank you for your help!

+7
javascript module plugins express
source share
2 answers

Do not waste energy on reinventing the wheel - use an existing solution called NPM modules :
You were right to feel that this was not so.

The best approach for him, IMO, is NPM NodeJs modules, which you could write yourself.

As for the binding procedure for the "get" / "post" / etc handler - you can still control the order in which the package is loaded, do not forget about it, so you can achieve all this in a "standard" way.

Edit # 1:

  • You can use private
  • For highly specialized subcomponents, I can recommend you look at the PassportJS project. It's about authentication with external platforms such as Facebook and Google, and each platform that you want to include in the project is a separate NPM module.
  • What makes you think that you cannot unload / reload modules at runtime? Yes, you just can.
+6
source share

I did not use it, but the Architect could substitute an account https://github.com/c9/architect

An architect is a simple but powerful framework for Node.js applications. Using Architect, you set up a simple configuration and tell architects which plugins you want to download. Each plugin registers itself with Architect, so other plugins can use its functions. Plugins can be supported as NPM packages, so you can drop them into other Architect applications.

0
source share

All Articles