Skip to main content

@std/http@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function serveDir
serveDir(): Promise<Response>

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

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

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

import { serveDir } from "@std/http/file_server";

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

The request to handle

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/http

Import symbol

import { serveDir } from "@std/http/file_server";
or

Import directly with a jsr specifier

import { serveDir } from "jsr:@std/http/file_server";