From 289e93cfca5e575efc10975698b3e9e7209dda02 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 12 Mar 2023 12:28:05 +1000 Subject: [PATCH] handle async safely --- routes/guestbook.js | 13 ++++++------- routes/homepage.js | 6 +++--- routes/proj.js | 8 ++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/routes/guestbook.js b/routes/guestbook.js index 2d06dfb..58ddefa 100644 --- a/routes/guestbook.js +++ b/routes/guestbook.js @@ -1,13 +1,12 @@ -const Helpers = require('../helpers'); const Sequelize = require('../models'); -const html_escape = require('html-escaper'); const xml = require('xml'); +const handler = require('express-async-handler'); const send_error = async (res, error) => { return res.redirect('/guestbook?error=' + encodeURIComponent(error)); }; -async function handler(req, res, next) { +async function guestbook(req, res, next) { try { const errors = req.query.error; @@ -179,8 +178,8 @@ async function rss(req, res) { } module.exports = (router) => { - router.get('/guestbook', handler); - router.post('/guestbook/submit', submit); - router.get('/guestbook/del/:id', del); - router.get('/guestbook.rss', rss); + router.get('/guestbook', handler(guestbook)); + router.post('/guestbook/submit', handler(submit)); + router.get('/guestbook/del/:id', handler(del)); + router.get('/guestbook.rss', handler(rss)); } \ No newline at end of file diff --git a/routes/homepage.js b/routes/homepage.js index a6bc72e..f494771 100644 --- a/routes/homepage.js +++ b/routes/homepage.js @@ -1,7 +1,7 @@ -const Helpers = require('../helpers'); const Sequelize = require('../models'); +const handler = require('express-async-handler'); -async function handler(req, res) { +async function index(req, res) { let gb_entries = await Sequelize.Guestbook.findAll({ limit: 5, @@ -31,6 +31,6 @@ async function handler(req, res) { } module.exports = (router) => { - router.get('/', handler); + router.get('/', handler(index)); router.get('/sources', (req, res) => {res.redirect('https://git.blek.codes/blek/homepage.js')}); } \ No newline at end of file diff --git a/routes/proj.js b/routes/proj.js index b64a544..a3e5624 100644 --- a/routes/proj.js +++ b/routes/proj.js @@ -1,6 +1,6 @@ -const Helpers = require('../helpers'); +const handler = require('express-async-handler'); -async function handler(req, res) { +async function project(req, res) { res.template( 'project.pug', { @@ -34,6 +34,6 @@ async function viewer(req, res) { } module.exports = (router) => { - router.get('/project', handler); - router.get('/project/:id', viewer); + router.get('/project', handler(project)); + router.get('/project/:id', handler(viewer)); } \ No newline at end of file