mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
df7e64fafa
refs #10790 - Moved /core/apps into core/frontend - Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes - Changed helper location in overrides - Moved /core/server/services/routing to /core/frontend/services - Moved /core/server/services/url to /core/frontend/services - Moved /core/server/data/meta to /core/frontend/meta - Moved /core/server/services/rss to /core/frontend/services - Moved /core/server/data/xml to /core/frontend/services
36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
const common = require('../../../../server/lib/common'),
|
|
urlUtils = require('../../../../server/lib/url-utils');
|
|
|
|
/**
|
|
* @description Middleware, which validates and interprets the page param e.g. /page/1
|
|
* @param {Object} req
|
|
* @param {Object} res
|
|
* @param {Function} next
|
|
* @param {Number} page
|
|
* @returns {*}
|
|
*/
|
|
module.exports = function handlePageParam(req, res, next, page) {
|
|
// routeKeywords.page: 'page'
|
|
const pageRegex = new RegExp('/page/(.*)?/'),
|
|
rssRegex = new RegExp('/rss/(.*)?/');
|
|
|
|
page = parseInt(page, 10);
|
|
|
|
if (page === 1) {
|
|
// CASE: page 1 is an alias for the collection index, do a permanent 301 redirect
|
|
// @TODO: this belongs into the rss router!
|
|
if (rssRegex.test(req.url)) {
|
|
return urlUtils.redirect301(res, req.originalUrl.replace(rssRegex, '/rss/'));
|
|
} else {
|
|
return urlUtils.redirect301(res, req.originalUrl.replace(pageRegex, '/'));
|
|
}
|
|
} else if (page < 1 || isNaN(page)) {
|
|
return next(new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.errors.pageNotFound')
|
|
}));
|
|
} else {
|
|
req.params.page = page;
|
|
return next();
|
|
}
|
|
};
|