From a91e46d70bf54b487abacb60432f6ed357188b67 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Mon, 23 Oct 2017 15:31:15 +0200 Subject: [PATCH] refactor: refactor home and about routes --- app.js | 2 +- routes/about.js | 10 ---------- routes/index.js | 17 +++-------------- routes/landingPage.js | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 25 deletions(-) delete mode 100644 routes/about.js create mode 100644 routes/landingPage.js diff --git a/app.js b/app.js index a9731de..f483c82 100644 --- a/app.js +++ b/app.js @@ -31,7 +31,7 @@ app.use(async function (req, res, next) { space: process.env.CF_SPACE, cda: process.env.CF_ACCESS_TOKEN, cpa: process.env.CF_PREVIEW_ACCESS_TOKEN, - editorialFeatures: true, + editorialFeatures: false, ...req.cookies.theExampleAppSettings } diff --git a/routes/about.js b/routes/about.js deleted file mode 100644 index ee8ca10..0000000 --- a/routes/about.js +++ /dev/null @@ -1,10 +0,0 @@ -const { getLandingPage } = require('../services/contentful') - -exports.getAbout = async (req, res, next) => { - const landingPage = await getLandingPage('about', - res.locals.currentLocale.code, - res.locals.currentApi.id - ) - res.render('landingPage', { title: 'About', landingPage }) -} - diff --git a/routes/index.js b/routes/index.js index 77ce2d5..ed4b3bb 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,25 +1,14 @@ const express = require('express') -const { getLandingPage } = require('../services/contentful') const { catchErrors } = require('../handlers/errorHandlers') const { getCourses, getCourse, getLesson, getCourseByCategory } = require('./courses') const { getSettings, postSettings } = require('./settings') const { getCategories } = require('./categories') const { getSitemap } = require('./sitemap') -const { getAbout } = require('./about') +const { getLandingPage } = require('./landingPage') const router = express.Router() /* GET the home landing page. */ -router.get('/', catchErrors(async function (req, res, next) { - const landingPage = await getLandingPage('home', res.locals.currentLocale.code, res.locals.currentApi.id) - let title = landingPage.fields.title - if (!title || landingPage.fields.slug === 'home') { - title = 'The Example App' - } - res.render('landingPage', { - title, - landingPage - }) -})) +router.get('/', catchErrors(getLandingPage)) /* Courses Routes */ router.get('/courses', catchErrors(getCourses)) @@ -39,6 +28,6 @@ router.get('/categories', catchErrors(getCategories)) router.get('/sitemap', catchErrors(getSitemap)) /* About Route */ -router.get('/about', catchErrors(getAbout)) +router.get('/about', catchErrors(getLandingPage)) module.exports = router diff --git a/routes/landingPage.js b/routes/landingPage.js new file mode 100644 index 0000000..d30319d --- /dev/null +++ b/routes/landingPage.js @@ -0,0 +1,14 @@ +const { getLandingPage } = require('../services/contentful') +const url = require('url') + +exports.getLandingPage = async (req, res, next) => { + let pathname = url.parse(req.url).pathname.split('/').filter(Boolean)[0] + pathname = pathname || 'home' + const landingPage = await getLandingPage( + pathname, + res.locals.currentLocale.code, + res.locals.currentApi.id + ) + res.render('landingPage', { title: pathname, landingPage }) +} +