Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a plugin system for a nodejs based MVC platform

I would like to be able to build functionality for my application in a plugin style system for a couple reasons:

  1. New projects can choose which plugins are necessary and not have code for functionality that's not needed
  2. Other developers can build plugins for the system without needing too much knowledge of the core workings.

I'm not really sure how to go about implementing this. I would like to have a plugins folder to host these separately but I guess my questions are:

  1. How do plugins interact with the core system?
  2. How does the folder structure work? Would each hold the standard MVC structure: controllers, services, models, views, etc?

I guess if anyone has a tutorial or some documentation relating to this technique that would be helpful. I've done a bit of searching but it's all a little too closely related to the actual code they're working with instead of the concept and I hadn't found anything specifically related to nodejs.

like image 601
Matt Avatar asked Sep 02 '12 03:09

Matt


People also ask

Does node js support MVC?

Sails is the most popular MVC framework for Node. js, designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture.

What are plugins in NodeJS?

A plugin is an extra piece of code that expands what a program can normally do. Web extensions are a good example of this. They leverage existing APIs, and build new functionality upon them. In certain circumstances, they can also be used to patch bugs and flaws that the main software provider has not yet addressed.

Is Express js a MVC framework?

Model-View-Controller frameworks feature an important design pattern that separates application logic into three parts: models, views, and controllers. The separation makes web development an easy, efficient, and scalable process. Express JS and Adonis JS are two of the most popular MVC frameworks based on Node.

What is plugin architecture?

A plug-in is a bundle that adds functionality to an application, called the host application, through some well-defined architecture for extensibility. This allows third-party developers to add functionality to an application without having access to the source code.


1 Answers

I suggest an approach similar to what I've done on the uptime project (https://github.com/fzaninotto/uptime/blob/master/app.js#L46):

  • trigger application events in critical parts of your application
  • add a 'plugins' section in the applicaition configuration
  • each plugin name must be a package name. The plugin packages should return either a callback, or an object with an init() function.
  • either way, inject to the plugins the objects they will need to run (configuration, connections, etc) when calling init(), or executing the callback.
  • plugin modules register listeners to the application events and modify it

Benefits:

  • lightweight
  • rely on npm for dependencies
  • don't reivent the wheel
like image 157
François Zaninotto Avatar answered Oct 27 '22 02:10

François Zaninotto