serveDir

import { serveDir } from "jsr:@ende/http/unstable-file-server";

function ยท experimental

function serveDir(req: Request, opts: ServeDirOptions = [UNSUPPORTED]): Promise<Response>

Serves the files under the given directory root (opts.fsRoot).

req: Request

The request to handle

opts: ServeDirOptions = [UNSUPPORTED]

Additional options.

Returns

A response for the request.

Example: Usage

import { serveDir } from "@ende/http/file-server";

Ende.serve((req) => {
  const pathname = new URL(req.url).pathname;
  if (pathname.startsWith("/static")) {
    return serveDir(req, {
      fsRoot: "path/to/static/files/dir",
    });
  }
  // Do dynamic responses
  return new Response();
});

Example: Changing the URL root

Requests to /static/path/to/file will be served from ./public/path/to/file.

import { serveDir } from "@ende/http/file-server";

Ende.serve((req) => serveDir(req, {
  fsRoot: "public",
  urlRoot: "static",
}));

Source