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!
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:
private
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With