Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/cav/deps.ts>fileServer.serveDir

A server framework for Deno
Go to Latest
function fileServer.serveDir
import { fileServer } from "https://deno.land/x/cav@0.2.3/deps.ts";
const { serveDir } = fileServer;

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

import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
import { serveDir } from "https://deno.land/std@0.224.0/http/file_server.ts";

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

Optionally you can pass urlRoot option. If it's specified that part is stripped from the beginning of the requested pathname.

import { serveDir } from "https://deno.land/std@0.224.0/http/file_server.ts";

// ...
serveDir(new Request("http://localhost/static/path/to/file"), {
  fsRoot: "public",
  urlRoot: "static",
});

The above example serves ./public/path/to/file for the request to /static/path/to/file.

Parameters

req: Request

The request to handle

optional
opts: ServeDirOptions = [UNSUPPORTED]