A web application allowing people to create an account, configure a profile, and share a list of URLs on that profile.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

69 lines
1.8 KiB

// page.js
// Copyright (C) 2021 Digital Telepresence, LLC
// License: Apache-2.0
'use strict';
const DTP_COMPONENT_NAME = 'page';
const express = require('express');
const { SiteController, SiteError } = require('../../lib/site-lib');
class PageController extends SiteController {
constructor (dtp) {
super(dtp, DTP_COMPONENT_NAME);
}
async start ( ) {
const { dtp } = this;
const { limiter: limiterService } = dtp.services;
const router = express.Router();
dtp.app.use('/page', router);
router.use(this.dtp.services.gabTV.channelMiddleware('mrjoeprich'));
router.use(async (req, res, next) => {
res.locals.currentView = 'home';
return next();
});
router.param('pageSlug', this.populatePageSlug.bind(this));
router.get('/:pageSlug',
limiterService.create(limiterService.config.page.getView),
this.getView.bind(this),
);
}
async populatePageSlug (req, res, next, pageSlug) {
const { page: pageService } = this.dtp.services;
try {
res.locals.page = await pageService.getBySlug(pageSlug);
if (!res.locals.page) {
throw new SiteError(404, 'Page not found');
}
return next();
} catch (error) {
this.log.error('failed to populate pageSlug', { pageSlug, error });
return next(error);
}
}
async getView (req, res, next) {
const { resource: resourceService } = this.dtp.services;
try {
await resourceService.recordView(req, 'Page', res.locals.page._id);
res.render('page/view');
} catch (error) {
this.log.error('failed to service page view', { pageId: res.locals.page._id, error });
return next(error);
}
}
}
module.exports = async (dtp) => {
let controller = new PageController(dtp);
return controller;
};