项目作者: MaxvandeLaar

项目描述 :
Load ExpressJS routes dynamic at runtime by placing them into a specified folder
高级语言: JavaScript
项目地址: git://github.com/MaxvandeLaar/express-plugable-routes.git
创建时间: 2018-08-04T08:09:49Z
项目社区:https://github.com/MaxvandeLaar/express-plugable-routes

开源协议:GNU Affero General Public License v3.0

下载


Express Plugable Routes

Load ExpressJS routes dynamic at runtime by placing them into a specified folder.

Install

npm install express-plugable-routes

yarn add express-plugable-routes

How to use

How to initialise the module

In your project, first declare the module
```javascript 1.8
const express = require(‘express’);
const ExpressPlugableRoutes = require(‘express-plugable-routes’);

const app = express();

// add your regular ExpressJS middleware and other logic such as:

/*

  • Add your 404 error logic here
    */
    function error404(req, res, next){
    //…
    }
    app.use(error404);

/*

  • Add your other error logic here
    */
    function errorHandler(req, res, next){
    //…
    }
    app.use(errorHandler);
    ```

Next, gather the required parameters and create a new instance

  1. const addonFolder = `${__dirname}/addons`; // This is the folder where you drop your runtime plugable routes in
  2. const watchFor = `${addonFolder}/**/config.json`; // The file to watch which contains all the required information about the new route. See Chokidar for more info on this
  3. const chokidarOpts = {ignored: /^\./, persistent: true}; // The options for Chokidar
  4. new ExpressPlugableRoutes(app, addonFolder, watchFor, chokidarOpts, error404, errorHandler);
  5. // That's it!
  6. module.exports = app;

How to list the added routes

To prevent running and requiring an external database, an ‘in-memory’ database (LokiJS) is used.

So getting a list of installed routes is easily achieved:

  1. const Loki = require('lokijs');
  2. const db = new Loki('ExpressPlugableRoutes');
  3. db.loadDatabase({}, function() {
  4. console.log(db.getCollection("plugins").data);
  5. });

Required in the ‘config.json’

The bare minimum should contain:

  1. {
  2. "expressPlugableRoutes": {
  3. "name": "myCustomRoutes", // name of the module
  4. "main": "index.js", // incoming js file to use
  5. "path": "/base-url-of-the-routes", // base url the module is mounted on
  6. "rootFolder": "myCustomRoutesFolder" // folder path relative to the addon folder up to the main file
  7. }
  8. }

Dependencies

License

GNU AGPL v3

Copyright (©) 2018 Max van de Laar
https://www.maxvandelaar.com