|
|
|
import Fastify from "fastify";
|
|
|
|
import FastifyStatic from "@fastify/static";
|
|
|
|
import path from "path";
|
|
|
|
import { ToDo } from "../lib/models.js";
|
|
|
|
import fs from "fs";
|
|
|
|
import template from "lodash/template.js";
|
|
|
|
|
|
|
|
const fastify = Fastify({
|
|
|
|
logger: true
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get("/todo", async (req, rep) => {
|
|
|
|
try {
|
|
|
|
const todo_list = await ToDo.all({});
|
|
|
|
|
|
|
|
// this is for exporing the problem of getting good error messages from templates
|
|
|
|
const tmpl_src = fs.readFileSync("templates/todo.html");
|
|
|
|
const tmpl = template(tmpl_src, {sourceURL: "templates/todo.html"});
|
|
|
|
|
|
|
|
console.log("FUNCTION-----\n", tmpl.toString());
|
|
|
|
|
|
|
|
const result = tmpl({todo_list});
|
|
|
|
|
|
|
|
rep.code(200)
|
|
|
|
.type("text/html")
|
|
|
|
.send(result);
|
|
|
|
} catch(error) {
|
|
|
|
console.error(error);
|
|
|
|
console.error(error.stack);
|
|
|
|
console.error(error.source);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get("/todo.html", async (req, rep) => {
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.register(FastifyStatic, {
|
|
|
|
root: path.join(path.resolve("."), 'static'),
|
|
|
|
prefix: '/', // optional: default '/'
|
|
|
|
constraints: {}, // optional: default {}
|
|
|
|
index: "index.html"
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fastify.listen({port: 3000});
|
|
|
|
} catch(err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|