Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plugin system for Node.JS

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

I was hoping to make things plug&play-able, so I started writing my own little plugin-system, whose file structure looks like this:

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, specific resources etc.

The standard hooks that app will call on an instance of MyPlugin 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, so good, but now things get more complicated (and they will always only get even more complicated), as I have to consider the sequence of module initialization (as they might attach themselves to the handler stack with use, get, post, etc.), and more...

Since I am still somewhat new to Node and Express, I started having doubts: Is there maybe already a good engine out there that does all that? I could not find one, which leads me to believe that this might actually be a bad idea? Maybe there is a more "node"- or "express"- kind of way to do things that I am overlooking?

Thanks for your help!

like image 204
Domi Avatar asked May 04 '14 08:05

Domi


1 Answers

Do not waste energy on re-inventing the wheel - use existing solution named NPM Modules :)
You were right to feel that this isn't the way.

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

As for the handler binding execution order of the "get"/"post"/etc - you still could control the package loading order, do not forget that, so you can achieve it all in a "standard" way.

Edit #1:

  1. You can use private
  2. For highly specialized sub-components example I can recommend you look at the PassportJS project. It's about authentication with external platforms such as Facebook and Google, and every platform you want to enable in a project is a separate NPM module.
  3. What makes you think you cannot unload/reload modules at runtime? Yes, you simply can.
like image 88
Poni Avatar answered Oct 06 '22 22:10

Poni