Maybe URLs like this? I think this'll need some reworking but it's a start.

main
Zed A. Shaw 10 months ago
parent ded0867b9e
commit 53d67f185b
  1. 27
      commands/app.js
  2. 4
      controllers/todo.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'),

@ -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 };

Loading…
Cancel
Save