diff --git a/commands/app.js b/commands/app.js index 7c919f2..be83324 100644 --- a/commands/app.js +++ b/commands/app.js @@ -18,6 +18,7 @@ let CONFIGURED = false; nunjucks.configure("templates", { watch: true }); const configure = async (fastify, opts) => { + const app = fastify(opts); // MAGIC: this is how you trick the importer to reload modules // that have changed. Since it uses a URL you can add a query @@ -25,23 +26,23 @@ const configure = async (fastify, opts) => { // tags the module as being "new" when it's still the same file // TODO: maybe use fs timestamps instead? // BUG: sometimes reload is too fast for vim and crashes - const controller = await import(`../controllers/todo.js?update=${new Date()}`); - - const handler = new controller.Todo(); - const app = fastify(opts); + const control_mod = await import(`../controllers/todo.js?update=${new Date()}`); // this is a sample that uses the handler we dynamic load // to handle the /todo but not sure how to work the actual // URL mappings for it. Also not sure about using classes - app.get("/todo", async (req, rep) => { - try { - await handler.get(req, rep); - } catch(error) { - console.error(error); - console.error(error.stack); - console.error(error.source); - } - }); + for(let [url, controller] of Object.entries(control_mod.default)) { + const handler = new controller(); + app.get(`/${url}`, async (req, rep) => { + try { + await handler.get(req, rep); + } catch(error) { + console.error(error); + console.error(error.stack); + console.error(error.source); + } + }); + } app.register(FastifyStatic, { root: path.join(path.resolve("."), 'static'), diff --git a/controllers/todo.js b/controllers/todo.js index 104b489..277c68a 100644 --- a/controllers/todo.js +++ b/controllers/todo.js @@ -14,3 +14,7 @@ export class Todo { rep.code(200).type("text/html").send(result); } } + +// you use the export default to set the route URL? +// I mean, it works not sure if I like it though +export default { todo: Todo };