Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback


The Deno Standard Library
Go to Latest
function serveDir
import { serveDir } from "";

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

import { serveDir } from "";

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

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

import { serveDir } from "";

// ...
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.


req: Request

The request to handle

opts: ServeDirOptions = [UNSUPPORTED]