import { serveStatic } from "https://deno.land/x/ayonli_jsext@v0.9.72/http.ts";
Serves static files from a file system directory or KV namespace (in Cloudflare Workers).
NOTE: In Node.js, this function requires Node.js v18.4.1 or above.
NOTE: In Cloudflare Workers, this function requires setting the
[site].bucket
option in the wrangler.toml
file.
Examples
Example 1
Example 1
import { serve, serveStatic } from "@ayonli/jsext/http";
// use `serve()` so this program runs in all environments
serve({
async fetch(req: Request, ctx) {
const { pathname } = new URL(req.url);
if (pathname.startsWith("/assets")) {
return await serveStatic(req, {
fsDir: "./assets",
kv: ctx.bindings?.__STATIC_CONTENT,
urlPrefix: "/assets",
});
}
return new Response("Hello, World!");
}
});
Example 2
Example 2
# wrangler.toml
[site]
bucket = "./assets"
Parameters
req: Request
optional
options: ServeStaticOptions = [UNSUPPORTED]