async/await

This commit is contained in:
Flummi
2019-04-26 09:23:16 +00:00
parent 6f6f3c8481
commit 06f665b220
4 changed files with 82 additions and 66 deletions

View File

@ -9,14 +9,16 @@ const template = fs.readFileSync("./views/index.hbs", "utf-8");
router.get(/^\/$/, async (req, res) => {
const db = await sql;
res.writeHead(200, { 'Content-Type': 'text/html' });
db.query(queries.items)
.then(items => {
const tpl = handlebars.compile(template);
res.end(tpl({ items: items, debug: JSON.stringify(req.url, null, 2) }));
}).catch(err => res.end(JSON.stringify( err ), 'utf-8'));
/*const tpl = handlebars.compile(template);
res.end(tpl());*/
try {
const rows = await db.query(queries.items);
const tpl = handlebars.compile(template);
res
.writeHead(200, { 'Content-Type': 'text/html' })
.end(tpl({ items: rows, debug: JSON.stringify(req.url, null, 2) }));
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), 'utf-8');
}
});