@ -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 ;
// 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" } ) ;
fastify . get ( "/todo.html" , async ( req , rep ) => {
console . log ( "FUNCTION-----\n" , tmpl . toString ( ) ) ;
const list = await ToDo . all ( { } ) ;
const tmpl _src = fs . readFileSync ( "templates/todo.html" ) ;
const result = tmpl ( { todo _list } ) ;
const tmpl = _ . template ( tmpl _src , ) ;
const result = tmpl ( { message : "Hello!" } ) ;
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 ) => {
rep . code ( 200 )
. type ( "text/html" )
. send ( result ) ;
} ) ;
} ) ;
fastify . register ( FastifyStatic , {
fastify . register ( FastifyStatic , {