@ -3,29 +3,36 @@ import FastifyStatic from "@fastify/static";
import path from "path" ;
import path from "path" ;
import { ToDo } from "../lib/models.js" ;
import { ToDo } from "../lib/models.js" ;
import fs from "fs" ;
import fs from "fs" ;
import _ from "lodash" ;
import template from "lodash/template.js " ;
const fastify = Fastify ( {
const fastify = Fastify ( {
logger : true
logger : true
} ) ;
} ) ;
fastify . get ( "/todo" , async ( request , reply ) => {
fastify . get ( "/todo" , async ( req , rep ) => {
const list = await ToDo . all ( { } ) ;
try {
const todo _list = await ToDo . all ( { } ) ;
return list ;
} ) ;
fastify . get ( "/todo.html" , async ( req , rep ) => {
const 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 _src = fs . readFileSync ( "templates/todo.html" ) ;
const tmpl = _ . template ( tmpl _src , ) ;
const tmpl = template ( tmpl _src , { sourceURL : "templates/todo.html" } ) ;
console . log ( "FUNCTION-----\n" , tmpl . toString ( ) ) ;
const result = tmpl ( { message : "Hello!" } ) ;
const result = tmpl ( { todo _list } ) ;
rep . code ( 200 )
rep . code ( 200 )
. type ( "text/html" )
. type ( "text/html" )
. send ( result ) ;
. 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 , {
fastify . register ( FastifyStatic , {