0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/services/routing/ParentRouter.js
kirrg001 691daa2a49 ParentRouter: Added wrapper for Express Router
refs #9601

- if you call `express.Router()`, the router's name is always "router"
- that is caused by the closure behaviour in express:
  - https://github.com/expressjs/express/blob/4.16.3/lib/router/index.js#L46
- Ghost creates a couple of express routers for dynamic routing
  - it depends how much you configure in your routes.yaml file
  - but every router is called "router"
  - this is hard to work with
- with this router wrapping logic, we are able to give each router an exact name

If you enable `DEBUG=ghost:services:routing:*`, you have seen this before

> ghost:services:routing:ParentRouter site: mountRouter: router +0ms

With the wrapper logic, you will see:

> ghost:services:routing:ParentRouter site: mountRouter: StaticPagesRouter +0ms

- furthermore, if you have to access the router stack (`app.router.stack`), you can easily identify and find router instances by name
2018-06-13 21:22:10 +02:00

107 lines
2.8 KiB
JavaScript

/**
* # Router
*
* A wrapper around express.Router
* Intended to be extended anywhere that routes need to be registered in Ghost
* Only allows for .use and .get at the moment - we don't have clear use-cases for anything else yet.
*/
const debug = require('ghost-ignition').debug('services:routing:ParentRouter'),
EventEmitter = require('events').EventEmitter,
express = require('express'),
_ = require('lodash'),
setPrototypeOf = require('setprototypeof'),
security = require('../../lib/security'),
urlService = require('../url'),
// This the route registry for the whole site
registry = require('./registry');
function GhostRouter(options) {
const router = express.Router(options);
function innerRouter(req, res, next) {
return innerRouter.handle(req, res, next);
}
setPrototypeOf(innerRouter, router);
Object.defineProperty(innerRouter, 'name', {
value: options.parent.name,
writable: false
});
innerRouter.parent = options.parent;
return innerRouter;
}
/**
* We expose a very limited amount of express.Router via specialist methods
*/
class ParentRouter extends EventEmitter {
constructor(name) {
super();
this.identifier = security.identifier.uid(10);
this.name = name;
this._router = GhostRouter({mergeParams: true, parent: this});
}
mountRouter(path, router) {
if (arguments.length === 1) {
router = path;
debug(this.name + ': mountRouter: ' + router.name);
this._router.use(router);
} else {
registry.setRoute(this.name, path);
debug(this.name + ': mountRouter: ' + router.name + ' at ' + path);
this._router.use(path, router);
}
}
mountRoute(path, controller) {
debug(this.name + ': mountRoute for', path, controller.name);
registry.setRoute(this.name, path);
this._router.get(path, controller);
}
unmountRoute(path) {
let indexToRemove = null;
_.each(this._router.stack, (item, index) => {
if (item.path === path) {
indexToRemove = index;
}
});
if (indexToRemove !== null) {
this._router.stack.splice(indexToRemove, 1);
}
}
router() {
return this._router;
}
getPermalinks() {
return this.permalinks;
}
getFilter() {
return this.filter;
}
/**
* Will return the full route including subdirectory.
* Do not use this function to mount routes for now, because the subdirectory is already mounted.
*/
getRoute(options) {
options = options || {};
return urlService.utils.createUrl(this.route.value, options.absolute, options.secure);
}
reset() {}
}
module.exports = ParentRouter;