f0bm/src/inc/routes/index.mjs
2019-05-07 20:23:43 +00:00

26 lines
668 B
JavaScript

import router from "../router";
import fs from "fs";
import sql from "../sql";
import handlebars from "handlebars";
import { queries } from "./inc/index";
const template = fs.readFileSync("./views/index.hbs", "utf-8");
router.get(/^\/(page\/[0-9]+)?$/, async (req, res) => {
const db = await sql.getConnection();
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');
}
db.end();
});