Creating routes from a file

@rogerweb hank for you detailed reply!

No, it is not CMS it is all static content. I also use this JSON to generate my frontend page. This way I can keep all content in one file.

Static solution is interesting but in my case every route host a project that have it’s own set of separate static files. When user navigate to let say /project1 then public files from /project1/public directory should be served under /project1. Here is my current full NodeJS server that utilize Express Sub-apps:

import express from "express";
import path from "path";
import fs from "fs";

let projects = JSON.parse(fs.readFileSync("./constants/projects.json"));

const codercat = express();
const port = 8081;
const __dirname = path.resolve();

// Serve main site static files first
codercat.use(express.static("out", { extensions: ["html"] }));

projects.forEach((app) => {
  const subApp = express();
  const appRoute = "/" + app.route;

  // Skip projects that do not have sub apps
  if (!app.public) return;

  app.public.forEach((path) =>
    subApp.use(express.static(path, { extensions: ["html"] }))
  );

  subApp.get("/", (_, res) => {
    res.sendFile(path.join(__dirname, app.entry));
  });

  // Each app will be server on sub-route
  // with its own configuration and set of public assets
  console.log("[+] Hosting", app.name, "under", appRoute);
  codercat.use(appRoute, subApp);
});

// Start server
codercat.listen(port, () => {
  console.log(`Listening on port ${port}`);
});

I think, compile solution would work for me. How do I create a route this way? Should I use the suggestion from Plug router compiling routes from file or there is a better way?

Do you think my NodeJS approach is similar to your third dynamic route suggestion?