Function serveDir

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

    Parameters

    • req: Request

      The request to handle

    • opts: ServeDirOptions = {}

      Additional options.

    Returns Promise<Response>

    A response for the request.

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

    Deno.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();
    });

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

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

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