An even more educational version of the Bandolier for Learn JS the Hard Way.
https://learnjsthehardway.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
927 B
43 lines
927 B
import Fastify from "fastify";
|
|
import FastifyStatic from "@fastify/static";
|
|
import path from "path";
|
|
import { ToDo } from "../lib/models.js";
|
|
import fs from "fs";
|
|
import _ from "lodash";
|
|
|
|
const fastify = Fastify({
|
|
logger: true
|
|
});
|
|
|
|
fastify.get("/todo", async (request, reply) => {
|
|
const list = await ToDo.all({});
|
|
|
|
return list;
|
|
});
|
|
|
|
fastify.get("/todo.html", async (req, rep) => {
|
|
const list = await ToDo.all({});
|
|
|
|
const tmpl_src = fs.readFileSync("templates/todo.html");
|
|
const tmpl = _.template(tmpl_src, );
|
|
|
|
const result = tmpl({message: "Hello!"});
|
|
|
|
rep.code(200)
|
|
.type("text/html")
|
|
.send(result);
|
|
});
|
|
|
|
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);
|
|
}
|
|
|