From 1e1d3488c23fb0f2d4670a045d837df7a8b9ca92 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 22 Jan 2023 23:09:32 -0500 Subject: [PATCH] Fix ordering of items before responding to request --- modules/_server.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/_server.js b/modules/_server.js index 105bf07..a66f9cb 100644 --- a/modules/_server.js +++ b/modules/_server.js @@ -29,32 +29,41 @@ app.set('view engine', 'html'); // A normal load of the root page app.get('/', (req, res) => { - if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${JSON.stringify(config)}`); + // if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${JSON.stringify(config)}`); res.render('index', { config: JSON.stringify(config) }); }); // A POST form submission to the root page -app.post('/', (req, res) => { - res.render('index', { config: JSON.stringify(config) }); +app.post('/', (req, response) => { if (req.body.start != undefined) { fn.commands.startup(); + response.render('index', { config: JSON.stringify(config) }); + return; } if (req.body.shutdown != undefined) { fn.commands.shutdown(); + response.render('index', { config: JSON.stringify(config) }); + return; } if (req.body.reload != undefined) { const updateAugerOffIntervalQuery = `UPDATE intervals SET value = '${2000 - req.body.feedRate}' WHERE key = 'auger_off'`; const updateAugerOnIntervalQuery = `UPDATE intervals SET value = '${req.body.feedRate}' WHERE key = 'auger_on'`; dbfn.run(updateAugerOffIntervalQuery).then(res => { if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger off interval updated: ${res.data.changes}`); - }).catch(err => console.log(`E: ${err}`)); - - dbfn.run(updateAugerOnIntervalQuery).then(res => { - if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger on interval updated: ${res.data.changes}`); + dbfn.run(updateAugerOnIntervalQuery).then(res => { + if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger on interval updated: ${res.data.changes}`); + fn.commands.refreshConfig().then(res => { + config = res.config; + response.render('index', { config: JSON.stringify(config) }); + return; + }); + }).catch(err => console.log(`E: ${err}`)); }).catch(err => console.log(`E: ${err}`)); } if (req.body.quit != undefined) { fn.commands.quit(); + response.render('index', { config: JSON.stringify(config) }); + return; } });