refactor: refactor routes
This commit is contained in:
committed by
Benedikt Rötsch
parent
8640483af3
commit
1e94188333
16
app.js
16
app.js
@@ -7,12 +7,7 @@ const logger = require('morgan')
|
|||||||
const cookieParser = require('cookie-parser')
|
const cookieParser = require('cookie-parser')
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
|
|
||||||
const index = require('./routes/index')
|
const routes = require('./routes/index')
|
||||||
const courses = require('./routes/courses')
|
|
||||||
const categories = require('./routes/categories')
|
|
||||||
const about = require('./routes/about')
|
|
||||||
const settings = require('./routes/settings')
|
|
||||||
const sitemap = require('./routes/sitemap')
|
|
||||||
|
|
||||||
const { initClient, getSpace } = require('./services/contentful')
|
const { initClient, getSpace } = require('./services/contentful')
|
||||||
const breadcrumb = require('./lib/breadcrumb')
|
const breadcrumb = require('./lib/breadcrumb')
|
||||||
@@ -36,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: false,
|
editorialFeatures: true,
|
||||||
...req.cookies.theExampleAppSettings
|
...req.cookies.theExampleAppSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,12 +103,7 @@ app.use(async function (req, res, next) {
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use('/', index)
|
app.use('/', routes)
|
||||||
app.use('/courses', courses)
|
|
||||||
app.use('/categories', categories)
|
|
||||||
app.use('/about', about)
|
|
||||||
app.use('/settings', settings)
|
|
||||||
app.use('/sitemap', sitemap)
|
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
const express = require('express')
|
|
||||||
const { getLandingPage } = require('../services/contentful')
|
const { getLandingPage } = require('../services/contentful')
|
||||||
const { catchErrors } = require('../handlers/errorHandlers')
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
/* GET the about page. */
|
exports.getAbout = async (req, res, next) => {
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
const landingPage = await getLandingPage('about',
|
||||||
const landingPage = await getLandingPage('about', res.locals.currentLocale.code, res.locals.currentApi.id)
|
res.locals.currentLocale.code,
|
||||||
|
res.locals.currentApi.id
|
||||||
|
)
|
||||||
res.render('landingPage', { title: 'About', landingPage })
|
res.render('landingPage', { title: 'About', landingPage })
|
||||||
}))
|
}
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
const express = require('express')
|
|
||||||
const { catchErrors } = require('../handlers/errorHandlers')
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
/* GET category listing. */
|
/* GET category listing. */
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
exports.getCategories = async (req, res, next) => {
|
||||||
res.render('categories', { title: 'Categories' })
|
res.render('categories', { title: 'Categories' })
|
||||||
}))
|
}
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
|
|||||||
@@ -1,20 +1,28 @@
|
|||||||
const express = require('express')
|
|
||||||
const {getCourses, getCourse, getCategories, getCoursesByCategory} = require('./../services/contentful')
|
const {getCourses, getCourse, getCategories, getCoursesByCategory} = require('./../services/contentful')
|
||||||
const { catchErrors } = require('../handlers/errorHandlers')
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
/* GET courses listing. */
|
exports.getCourses = async (req, res, next) => {
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
|
||||||
// we get all the entries with the content type `course`
|
// we get all the entries with the content type `course`
|
||||||
let courses = []
|
let courses = []
|
||||||
let categories = []
|
let categories = []
|
||||||
courses = await getCourses(res.locals.currentLocale.code, res.locals.currentApi.id)
|
courses = await getCourses(res.locals.currentLocale.code, res.locals.currentApi.id)
|
||||||
categories = await getCategories(res.locals.currentLocale.code, res.locals.currentApi.id)
|
categories = await getCategories(res.locals.currentLocale.code, res.locals.currentApi.id)
|
||||||
res.render('courses', { title: `All Courses (${courses.length})`, categories, courses })
|
res.render('courses', { title: `All Courses (${courses.length})`, categories, courses })
|
||||||
}))
|
}
|
||||||
|
|
||||||
/* GET courses listing by category. */
|
exports.getCourse = async (req, res, next) => {
|
||||||
router.get('/categories/:category', catchErrors(async function (req, res, next) {
|
let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentApi.id)
|
||||||
|
const lessons = course.fields.lessons
|
||||||
|
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
||||||
|
const lesson = lessons[lessonIndex]
|
||||||
|
const cookie = req.cookies.visitedLessons
|
||||||
|
let visitedLessons = cookie || []
|
||||||
|
visitedLessons.push(course.sys.id)
|
||||||
|
visitedLessons = [...new Set(visitedLessons)]
|
||||||
|
res.cookie('visitedLessons', visitedLessons, { maxAge: 900000, httpOnly: true })
|
||||||
|
res.render('course', {title: course.fields.title, course, lesson, lessons, lessonIndex, visitedLessons})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getCoursesByCategory = async (req, res, next) => {
|
||||||
// we get all the entries with the content type `course` filtered by a category
|
// we get all the entries with the content type `course` filtered by a category
|
||||||
let courses = []
|
let courses = []
|
||||||
let categories = []
|
let categories = []
|
||||||
@@ -27,26 +35,10 @@ router.get('/categories/:category', catchErrors(async function (req, res, next)
|
|||||||
console.log('Error ', e)
|
console.log('Error ', e)
|
||||||
}
|
}
|
||||||
res.render('courses', { title: `${activeCategory.fields.title} (${courses.length})`, categories, courses })
|
res.render('courses', { title: `${activeCategory.fields.title} (${courses.length})`, categories, courses })
|
||||||
}))
|
}
|
||||||
|
|
||||||
/* GET course detail. */
|
|
||||||
const courseRoute = catchErrors(async function (req, res, next) {
|
|
||||||
let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentApi.id)
|
|
||||||
const lessons = course.fields.lessons
|
|
||||||
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
|
||||||
const lesson = lessons[lessonIndex]
|
|
||||||
const cookie = req.cookies.visitedLessons
|
|
||||||
let visitedLessons = cookie || []
|
|
||||||
visitedLessons.push(course.sys.id)
|
|
||||||
visitedLessons = [...new Set(visitedLessons)]
|
|
||||||
res.cookie('visitedLessons', visitedLessons, { maxAge: 900000, httpOnly: true })
|
|
||||||
res.render('course', {title: course.fields.title, course, lesson, lessons, lessonIndex, visitedLessons})
|
|
||||||
})
|
|
||||||
router.get('/:slug', courseRoute)
|
|
||||||
router.get('/:slug/lessons', courseRoute)
|
|
||||||
|
|
||||||
/* GET course lesson detail. */
|
/* GET course lesson detail. */
|
||||||
router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next) {
|
exports.getLesson = async (req, res, next) => {
|
||||||
let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentApi.id)
|
let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentApi.id)
|
||||||
const lessons = course.fields.lessons
|
const lessons = course.fields.lessons
|
||||||
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
||||||
@@ -65,6 +57,5 @@ router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next)
|
|||||||
nextLesson,
|
nextLesson,
|
||||||
visitedLessons
|
visitedLessons
|
||||||
})
|
})
|
||||||
}))
|
}
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const { getLandingPage } = require('../services/contentful')
|
const { getLandingPage } = require('../services/contentful')
|
||||||
const { catchErrors } = require('../handlers/errorHandlers')
|
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 router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
/* GET the home landing page. */
|
/* GET the home landing page. */
|
||||||
@@ -16,4 +21,24 @@ router.get('/', catchErrors(async function (req, res, next) {
|
|||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
/* Courses Routes */
|
||||||
|
router.get('/courses', catchErrors(getCourses))
|
||||||
|
router.get('/courses/categories/:category', catchErrors(getCourseByCategory))
|
||||||
|
router.get('/courses/:slug', catchErrors(getCourse))
|
||||||
|
router.get('/courses/:slug/lessons', catchErrors(getCourse))
|
||||||
|
router.get('/courses/:cslug/lessons/:lslug', catchErrors(getLesson))
|
||||||
|
|
||||||
|
/* Settings Routes */
|
||||||
|
router.get('/settings', catchErrors(getSettings))
|
||||||
|
router.post('/settings', catchErrors(postSettings))
|
||||||
|
|
||||||
|
/* Categories Route */
|
||||||
|
router.get('/categories', catchErrors(getCategories))
|
||||||
|
|
||||||
|
/* Sitemap Route */
|
||||||
|
router.get('/sitemap', catchErrors(getSitemap))
|
||||||
|
|
||||||
|
/* About Route */
|
||||||
|
router.get('/about', catchErrors(getAbout))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
const express = require('express')
|
|
||||||
const { createClient } = require('contentful')
|
const { createClient } = require('contentful')
|
||||||
const { getSpace } = require('./../services/contentful')
|
const { getSpace } = require('./../services/contentful')
|
||||||
const { catchErrors } = require('../handlers/errorHandlers')
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
async function renderSettings (res, opts) {
|
async function renderSettings (res, opts) {
|
||||||
// Get connectred space to display the space name on top of the settings
|
// Get connectred space to display the space name on top of the settings
|
||||||
@@ -24,15 +21,15 @@ async function renderSettings (res, opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* GET settings page. */
|
/* GET settings page. */
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
exports.getSettings = async (req, res, next) => {
|
||||||
const { settings } = res.locals
|
const { settings } = res.locals
|
||||||
await renderSettings(res, {
|
await renderSettings(res, {
|
||||||
settings
|
settings
|
||||||
})
|
})
|
||||||
}))
|
}
|
||||||
|
|
||||||
/* POST settings page. */
|
/* POST settings page. */
|
||||||
router.post('/', catchErrors(async function (req, res, next) {
|
exports.postSettings = async (req, res, next) => {
|
||||||
const errorList = []
|
const errorList = []
|
||||||
const { space, cda, cpa, editorialFeatures } = req.body
|
const { space, cda, cpa, editorialFeatures } = req.body
|
||||||
const settings = {
|
const settings = {
|
||||||
@@ -138,6 +135,5 @@ router.post('/', catchErrors(async function (req, res, next) {
|
|||||||
hasErrors: errorList.length > 0,
|
hasErrors: errorList.length > 0,
|
||||||
success: errorList.length === 0
|
success: errorList.length === 0
|
||||||
})
|
})
|
||||||
}))
|
}
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
const express = require('express')
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
/* GET sitemap page. */
|
/* GET sitemap page. */
|
||||||
router.get('/', function (req, res, next) {
|
exports.getSitemap = async (req, res, next) => {
|
||||||
res.render('sitemap', { title: 'Sitemap' })
|
res.render('sitemap', { title: 'Sitemap' })
|
||||||
})
|
}
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
|
|||||||
Reference in New Issue
Block a user