Ghost/core/server/services/routing/RSSRouter.js
Naz Gargol abda6e6338
Migrated to use url-utils from Ghost-SDK (#10787)
closes #10773

- The refactoring is a substitute for `urlService.utils` used previously throughout the codebase and now extracted into the separate module in Ghost-SDK
- Added url-utils stubbing utility for test suites
- Some tests had to be refactored to avoid double mocks (when url's are being reset inside of rested 'describe' groups)
2019-06-18 15:13:55 +02:00

53 lines
1.4 KiB
JavaScript

const ParentRouter = require('./ParentRouter');
const urlUtils = require('../../lib/url-utils');
const controllers = require('./controllers');
const middlewares = require('./middlewares');
/**
* @description RSS Router, which should be used as a sub-router in other routes.
*
* "/rss" -> RSS Router
*/
class RSSRouter extends ParentRouter {
constructor() {
super('RSSRouter');
this.route = {value: '/rss/'};
this._registerRoutes();
}
/**
* @description Register all routes of this router.
* @private
*/
_registerRoutes() {
this.mountRoute(this.route.value, controllers.rss);
// REGISTER: pagination
this.router().param('page', middlewares.pageParam);
// REGISTER: actual rss route
this.mountRoute(urlUtils.urlJoin(this.route.value, ':page(\\d+)'), controllers.rss);
// REGISTER: redirect rule
this.mountRoute('/feed/', this._redirectFeedRequest.bind(this));
}
/**
* @description Simple controller function to redirect /rss to /feed
* @param {Object} req
* @param {Object}res
* @private
*/
_redirectFeedRequest(req, res) {
urlUtils
.redirect301(
res,
urlUtils.urlJoin(urlUtils.getSubdir(), req.baseUrl, this.route.value)
);
}
}
module.exports = RSSRouter;