This Val provides a reusable static file server that can be imported into other Val Town projects to serve static assets from the importing project's directory structure.
index.html serving for directory requestsImport the createStaticServer function into your HTTP val:
import { createStaticServer } from "https://esm.town/v/peterqliu/static/server.ts";
// Create a static server that serves files from your project
const staticServer = createStaticServer(import.meta.url);
export default async function (req: Request): Promise<Response> {
return staticServer(req);
}
import { Hono } from "https://esm.sh/hono@3.11.7";
import { createStaticServer } from "https://esm.town/v/peterqliu/static/server.ts";
const app = new Hono();
const staticServer = createStaticServer(import.meta.url);
// Serve API routes
app.get("/api/hello", (c) => c.json({ message: "Hello World" }));
// Serve static files for all other routes
app.get("*", async (c) => {
return staticServer(c.req.raw);
});
export default app.fetch;
import.meta.url to determine the correct base directory./style.css from /subdir/page.html)index.html for directory requestsCreates a static file server function.
Parameters:
importMetaUrl: The import.meta.url from your importing projectReturns:
(req: Request) => Promise<Response>: A function that handles HTTP requests