简体   繁体   中英

Node.js - How to write plugins for application?

So I have a node.js application that I want to have plugins that can one or several of the following:

  1. Data importer
  2. Data outputer
  3. Data storage (mongodb)
  4. View (express/jade)

So say plugin A is a data storage and plugin B is a data outputer and view plugin that requires plugin A ?

Is there any existing framework that can solve this for me?

I think I know how to do it kind of with require and some 'magic', but I have requirements:

  • Plugins should register with the main application so the application can send events or call methods on the plugin.
  • Should be easy to pull in and out plugins
  • Should be able to validate that you have the dependency plugins installed for a plugin.

All you have to do is to write your plugin as simple Node.js module and, as you already mentioned, require it in your main application.

Your plugin has to export an activate -function which you call from your main app.

To register functionality (or the plugin itself) in the main application, you could write a node module, that the plugin developer must add to it's developer dependencies. Let's call it plugin-interface-module for now. These module should have a register function which you call inside of your activate -function in the plugin.

Your main app also requires these plugin-interface-module and retrieves all registered plugins from it. Required modules are globaly cached, like singletons, so your main app is able to get the list of registered plugins.

Take a look ad VS Vode ( https://code.visualstudio.com ). It's written in typescript and uses extensions in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM