refactor: refactor home and about routes

This commit is contained in:
Khaled Garbaya
2017-10-23 15:31:15 +02:00
committed by Benedikt Rötsch
parent 1e94188333
commit a91e46d70b
4 changed files with 18 additions and 25 deletions

2
app.js
View File

@@ -31,7 +31,7 @@ app.use(async function (req, res, next) {
space: process.env.CF_SPACE, space: process.env.CF_SPACE,
cda: process.env.CF_ACCESS_TOKEN, cda: process.env.CF_ACCESS_TOKEN,
cpa: process.env.CF_PREVIEW_ACCESS_TOKEN, cpa: process.env.CF_PREVIEW_ACCESS_TOKEN,
editorialFeatures: true, editorialFeatures: false,
...req.cookies.theExampleAppSettings ...req.cookies.theExampleAppSettings
} }

View File

@@ -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 })
}

View File

@@ -1,25 +1,14 @@
const express = require('express') const express = require('express')
const { getLandingPage } = require('../services/contentful')
const { catchErrors } = require('../handlers/errorHandlers') const { catchErrors } = require('../handlers/errorHandlers')
const { getCourses, getCourse, getLesson, getCourseByCategory } = require('./courses') const { getCourses, getCourse, getLesson, getCourseByCategory } = require('./courses')
const { getSettings, postSettings } = require('./settings') const { getSettings, postSettings } = require('./settings')
const { getCategories } = require('./categories') const { getCategories } = require('./categories')
const { getSitemap } = require('./sitemap') const { getSitemap } = require('./sitemap')
const { getAbout } = require('./about') const { getLandingPage } = require('./landingPage')
const router = express.Router() const router = express.Router()
/* GET the home landing page. */ /* GET the home landing page. */
router.get('/', catchErrors(async function (req, res, next) { router.get('/', catchErrors(getLandingPage))
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
})
}))
/* Courses Routes */ /* Courses Routes */
router.get('/courses', catchErrors(getCourses)) router.get('/courses', catchErrors(getCourses))
@@ -39,6 +28,6 @@ router.get('/categories', catchErrors(getCategories))
router.get('/sitemap', catchErrors(getSitemap)) router.get('/sitemap', catchErrors(getSitemap))
/* About Route */ /* About Route */
router.get('/about', catchErrors(getAbout)) router.get('/about', catchErrors(getLandingPage))
module.exports = router module.exports = router

14
routes/landingPage.js Normal file
View File

@@ -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 })
}