From 7c3ff9b699e06eaa2d879dcf1f35651119019b5b Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 12 Mar 2023 01:54:53 +1000 Subject: [PATCH] refactor template loading mechanism --- routes/about.js | 12 ------------ routes/admin.js | 6 +++--- routes/articles.js | 8 ++++---- routes/guestbook.js | 8 ++++---- routes/pages.js | 7 +++++++ routes/proj.js | 41 ++++++++++++++++++----------------------- 6 files changed, 36 insertions(+), 46 deletions(-) delete mode 100644 routes/about.js diff --git a/routes/about.js b/routes/about.js deleted file mode 100644 index cb337d3..0000000 --- a/routes/about.js +++ /dev/null @@ -1,12 +0,0 @@ -const Helpers = require('../helpers'); -const handler = require('express-async-handler'); - -async function about(req, res) { - res.send(await Helpers.ViewLoader.load('about.pug', { - current_route: req.originalUrl - })); -} - -module.exports = (router) => { - router.get('/about', handler(about)); -} \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index 80df6cf..d1096a7 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -41,11 +41,11 @@ async function panel(req, res) { const articles = await db.Article.findAll({where: {hidden: true}}); - res.send(await Helpers.ViewLoader.load('admin/panel.pug', { + res.template('admin/panel.pug', { current_route: req.originalUrl, gb_records, access_level: user.accessLevel - })); + }); return; } @@ -74,7 +74,7 @@ async function gb_api(req, res) { } async function article_new(req, res) { - res.send(await Helpers.ViewLoader.load('articles/new.pug')) + res.template('articles/new.pug') } module.exports = (router) => { diff --git a/routes/articles.js b/routes/articles.js index 9ecc0a4..70d03dd 100644 --- a/routes/articles.js +++ b/routes/articles.js @@ -7,14 +7,14 @@ async function articles(req, res) { const articles = await Sequelize.Article.findAll(); - res.send(await Helpers.ViewLoader.load('articles/articles.pug', { + res.template('articles/articles.pug', { current_route: res.originalUrl, articles - })); + }); } async function new_article(req, res) { - res.send(await Helpers.ViewLoader.load('admin/data_edit.pug', { + res.template('admin/data_edit.pug', { current_route: req.originalUrl, data: { 'title': { @@ -39,7 +39,7 @@ async function new_article(req, res) { title: 'New article', endpoint: '/articles/new', pref_method: 'POST' - })); + }); } async function new_article_post(req, res) { diff --git a/routes/guestbook.js b/routes/guestbook.js index d877589..2d06dfb 100644 --- a/routes/guestbook.js +++ b/routes/guestbook.js @@ -23,7 +23,7 @@ async function handler(req, res, next) { if (!data) throw new Error('Failed to get guestbook entries'); - res.send(await Helpers.ViewLoader.load('guestbook.pug', { + res.template('guestbook.pug', { current_route: req.originalUrl, ip: req.ip, data, @@ -31,7 +31,7 @@ async function handler(req, res, next) { name: req.session.gb_name, email: req.session.gb_email, hidemail: req.session.gb_hidemail - })); + }); return; } catch (err) { next(err); @@ -104,11 +104,11 @@ async function submit(req, res, next) { time: Math.floor(Date.now() / 1000) }); if (!data) { - res.send(await Helpers.ViewLoader.load('guestbook.pug', { + res.template('guestbook.pug', { current_route: req.originalUrl, ip: req.ip, errors: 'Could not create a new record' - })); + }); } res.redirect('/guestbook#gb_entry_' + data.id); diff --git a/routes/pages.js b/routes/pages.js index 0ed930c..1fe8197 100644 --- a/routes/pages.js +++ b/routes/pages.js @@ -9,7 +9,14 @@ async function resume(req, res) { console.log(process.env.APP_DEBUG); } +async function about(req, res) { + res.template('about.pug', { + current_route: req.originalUrl + }); +} + module.exports = (router) => { router.get('/services', handler(services)) router.get('/resume', handler(resume)) + router.get('/about', handler(about)); } \ No newline at end of file diff --git a/routes/proj.js b/routes/proj.js index 3c45c4e..b64a544 100644 --- a/routes/proj.js +++ b/routes/proj.js @@ -1,13 +1,11 @@ const Helpers = require('../helpers'); async function handler(req, res) { - res.send( - await Helpers.ViewLoader.load( - 'project.pug', - { - current_route: '/project' - } - ) + res.template( + 'project.pug', + { + current_route: '/project' + } ); return; } @@ -16,25 +14,22 @@ async function viewer(req, res) { const id = req.params.id; const view = 'projects/' + id + '.pug'; if (!(await Helpers.ViewLoader.exists(view))) { - res.status(404).send( - await Helpers.ViewLoader.load( - 'error.pug', - { - current_route: req.originalUrl, - error: '404 Not Found', - message: 'Requested project does not exists. Are you sure this is the valid link?' - } - ) + res.status(404).template( + 'error.pug', + { + current_route: req.originalUrl, + error: '404 Not Found', + message: 'Requested project does not exists. Are you sure this is the valid link?' + } + ); return; } - res.send( - await Helpers.ViewLoader.load( - view, - { - current_route: req.originalUrl - } - ) + res.template( + view, + { + current_route: req.originalUrl + } ) }