deno.com
本頁面

檔案系統 API

Deno Deploy 支援 Deno 中可用的部分檔案系統 API。這些檔案系統 API 可以存取您部署中的靜態檔案。靜態檔案例如

  • 如果您透過 GitHub 整合部署,則為您 GitHub 儲存庫中的檔案。
  • 遊樂場部署中的進入點檔案。

可用的 API 有

Deno.cwd 跳到標題

Deno.cwd() 傳回您部署的目前工作目錄。它位於您部署的根目錄的根目錄。例如,如果您透過 GitHub 整合部署,則目前工作目錄是您 GitHub 儲存庫的根目錄。

Deno.readDir 跳到標題

Deno.readDir() 允許您列出目錄的內容。

此函式與 Deno 完全相容。

function Deno.readDir(path: string | URL): AsyncIterable<DirEntry>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例列出目錄的內容,並以 JSON 物件的形式在回應主體中傳回此列表。

async function handler(_req) {
  // List the posts in the `blog` directory located at the root
  // of the repository.
  const posts = [];
  for await (const post of Deno.readDir(`./blog`)) {
    posts.push(post);
  }

  // Return JSON.
  return new Response(JSON.stringify(posts, null, 2), {
    headers: {
      "content-type": "application/json",
    },
  });
}

Deno.serve(handler);

Deno.readFile 跳到標題

Deno.readFile() 允許您將檔案完整讀取到記憶體中。

函式定義與 Deno 類似,但目前不支援 ReadFileOptions。未來將新增支援。

function Deno.readFile(path: string | URL): Promise<Uint8Array>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例將檔案內容以位元組陣列的形式讀取到記憶體中,然後將其作為回應主體傳回。

async function handler(_req) {
  // Let's read the README.md file available at the root
  // of the repository to explore the available methods.

  // Relative paths are relative to the root of the repository
  const readmeRelative = await Deno.readFile("./README.md");
  // Absolute paths.
  // The content of the repository is available under at Deno.cwd().
  const readmeAbsolute = await Deno.readFile(`${Deno.cwd()}/README.md`);
  // File URLs are also supported.
  const readmeFileUrl = await Deno.readFile(
    new URL(`file://${Deno.cwd()}/README.md`),
  );

  // Decode the Uint8Array as string.
  const readme = new TextDecoder().decode(readmeRelative);
  return new Response(readme);
}

Deno.serve(handler);

注意:若要使用此功能,您必須將 GitHub 儲存庫連結到您的專案。

Deno Deploy 支援 Deno.readFile API,以從檔案系統讀取靜態資源。這對於提供靜態資源(例如圖片、樣式表和 JavaScript 檔案)非常有用。本指南示範如何使用此功能。

想像 GitHub 儲存庫上有以下檔案結構:

├── mod.ts
└── style.css

mod.ts 的內容

async function handleRequest(request: Request): Promise<Response> {
  const { pathname } = new URL(request.url);

  // This is how the server works:
  // 1. A request comes in for a specific asset.
  // 2. We read the asset from the file system.
  // 3. We send the asset back to the client.

  // Check if the request is for style.css.
  if (pathname.startsWith("/style.css")) {
    // Read the style.css file from the file system.
    const file = await Deno.readFile("./style.css");
    // Respond to the request with the style.css file.
    return new Response(file, {
      headers: {
        "content-type": "text/css",
      },
    });
  }

  return new Response(
    `<html>
      <head>
        <link rel="stylesheet" href="style.css" />
      </head>
      <body>
        <h1>Example</h1>
      </body>
    </html>`,
    {
      headers: {
        "content-type": "text/html; charset=utf-8",
      },
    },
  );
}

Deno.serve(handleRequest);

提供給 Deno.readFile API 的路徑是相對於儲存庫的根目錄。您也可以指定絕對路徑,如果它們在 Deno.cwd 內。

Deno.readTextFile 跳到標題

此函式與 Deno.readFile 類似,不同之處在於它將檔案內容解碼為 UTF-8 字串。

function Deno.readTextFile(path: string | URL): Promise<string>

範例 跳到標題

此範例將文字檔讀取到記憶體中,並將內容作為回應主體傳回。

async function handler(_req) {
  const readme = await Deno.readTextFile("./README.md");
  return new Response(readme);
}

Deno.serve(handler);

Deno.open 跳到標題

Deno.open() 允許您開啟檔案,並傳回檔案控制代碼。然後可以使用此檔案控制代碼來讀取檔案的內容。請參閱 Deno.File 以取得有關檔案控制代碼上可用方法的資訊。

函式定義與 Deno 類似,但目前不支援 OpenOptions。未來將新增支援。

function Deno.open(path: string | URL): Promise<Deno.File>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例開啟檔案,然後將內容作為回應主體串流。

async function handler(_req) {
  // Open the README.md file available at the root of the repository.
  const file = await Deno.open("./README.md");

  // Use the `readable` property, which is a `ReadableStream`. This will
  // automatically close the file handle when the response is done sending.
  return new Response(file.readable);
}

Deno.serve(handler);

注意

當您如下所示迭代檔案串流時,檔案描述元將在迭代結束時自動關閉。無需手動關閉檔案描述元:const iterator = fd.readable[Symbol.asyncIterator]();

Deno.File 跳到標題

Deno.File 是從 Deno.open() 傳回的檔案控制代碼。它可用於使用 read() 方法讀取檔案的區塊。可以使用 close() 方法關閉檔案控制代碼。

介面與 Deno 類似,但它不支援寫入檔案或搜尋。未來將新增對後者的支援。

class File {
  readonly rid: number;

  close(): void;
  read(p: Uint8Array): Promise<number | null>;
}

路徑可以是相對或絕對路徑。它也可以是 file: URL。

Deno.File#read() 跳到標題

read 方法用於讀取檔案的區塊。應將其傳遞緩衝區以將資料讀取到其中。如果已到達檔案末尾,則傳回讀取的位元組數或 null

function read(p: Uint8Array): Promise<number | null>;

Deno.File#close() 跳到標題

close 方法用於關閉檔案控制代碼。關閉控制代碼將中斷所有正在進行的讀取。

function close(): void;

Deno.stat 跳到標題

Deno.stat() 讀取檔案系統項目的中繼資料。它傳回 Deno.FileInfo 物件。符號連結會被追蹤。

函式定義與 Deno 相同。它不傳回修改時間、存取時間或建立時間值。

function Deno.stat(path: string | URL): Promise<Deno.FileInfo>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例取得檔案的大小,並將結果作為回應主體傳回。

async function handler(_req) {
  // Get file info of the README.md at the root of the repository.
  const info = await Deno.stat("./README.md");

  // Get the size of the file in bytes.
  const size = info.size;

  return new Response(`README.md is ${size} bytes large`);
}

Deno.serve(handler);

Deno.lstat 跳到標題

Deno.lstat()Deno.stat() 類似,但它不追蹤符號連結。

函式定義與 Deno 相同。它不傳回修改時間、存取時間或建立時間值。

function Deno.lstat(path: string | URL): Promise<Deno.FileInfo>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

Deno.FileInfo 跳到標題

Deno.FileInfo 介面用於表示檔案系統項目的中繼資料。它由 Deno.stat()Deno.lstat() 函式傳回。它可以表示檔案、目錄或符號連結。

在 Deno Deploy 中,僅檔案類型和大小屬性可用。大小屬性的行為方式與 Linux 上的行為方式相同。

interface FileInfo {
  isDirectory: boolean;
  isFile: boolean;
  isSymlink: boolean;
  size: number;
}

Deno.realPath 跳到標題

Deno.realPath() 在追蹤符號連結後,傳回檔案的已解析絕對路徑。

函式定義與 Deno 相同。

function Deno.realPath(path: string | URL): Promise<string>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例呼叫 Deno.realPath() 以取得儲存庫根目錄中檔案的絕對路徑。結果將作為回應主體傳回。

async function handler(_req) {
  const path = await Deno.realPath("./README.md");

  return new Response(`The fully resolved path for ./README.md is ${path}`);
}

Deno.serve(handler);

Deno.readLink() 傳回符號連結的目標路徑。

函式定義與 Deno 相同。

function Deno.readLink(path: string | URL): Promise<string>

路徑可以是相對或絕對路徑。它也可以是 file: URL。

範例 跳到標題

此範例呼叫 Deno.readLink() 以取得儲存庫根目錄中檔案的絕對路徑。結果將作為回應主體傳回。

async function handler(_req) {
  const path = await Deno.readLink("./my_symlink");

  return new Response(`The target path for ./my_symlink is ${path}`);
}

Deno.serve(handler);

您找到需要的資訊了嗎?

隱私權政策