Public
Viewing readonly version: 182View latest version
It's quite common to want to serve static files from your backend server to the frontend.
import { servePublicFile } from "https://esm.town/v/stevekrouse/utils@179-main/serve-public/index.ts";
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
if (url.pathname === "/") {
return servePublicFile("/public/index.html", import.meta.url);
} else if (url.pathname.startsWith("/public")) {
return servePublicFile(url.pathname, import.meta.url);
} else {
// normal server code here
}
}
This requires a couple steps:
- Handling requests for those files in your server
- Reading those files in the right place in the project
- Adding the correct content-type