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.
52 lines
1.1 KiB
52 lines
1.1 KiB
9 months ago
|
import Fastify from "fastify";
|
||
|
import FastifyStatic from "@fastify/static";
|
||
|
import path from "path";
|
||
|
import { ToDo } from "../lib/models.js";
|
||
|
import fs from "fs";
|
||
9 months ago
|
import nunjucks from "nunjucks";
|
||
9 months ago
|
|
||
9 months ago
|
export const description = "Runs your app.";
|
||
|
|
||
|
export const options = [];
|
||
|
|
||
|
export const required = [];
|
||
|
|
||
9 months ago
|
const fastify = Fastify({
|
||
|
logger: true
|
||
|
});
|
||
|
|
||
9 months ago
|
|
||
9 months ago
|
fastify.get("/todo", async (req, rep) => {
|
||
|
try {
|
||
|
const todo_list = await ToDo.all({});
|
||
9 months ago
|
|
||
9 months ago
|
// this is for exporing the problem of getting good error messages from templates
|
||
9 months ago
|
const result = nunjucks.render("templates/todo.html",
|
||
|
{todo_list, your_todos: "Your Todos"});
|
||
9 months ago
|
|
||
9 months ago
|
rep.code(200)
|
||
|
.type("text/html")
|
||
|
.send(result);
|
||
|
} catch(error) {
|
||
|
console.error(error);
|
||
|
console.error(error.stack);
|
||
|
console.error(error.source);
|
||
|
}
|
||
|
});
|
||
|
|
||
9 months ago
|
fastify.register(FastifyStatic, {
|
||
|
root: path.join(path.resolve("."), 'static'),
|
||
|
prefix: '/', // optional: default '/'
|
||
|
constraints: {}, // optional: default {}
|
||
|
index: "index.html"
|
||
|
})
|
||
|
|
||
9 months ago
|
export const main = async (arg, opts) => {
|
||
|
try {
|
||
|
await fastify.listen({port: 3000});
|
||
|
} catch(err) {
|
||
|
fastify.log.error(err);
|
||
|
process.exit(1);
|
||
|
}
|
||
9 months ago
|
}
|