deno.com
本頁面內容

deno serve

命令列用法

deno serve [OPTIONS] [SCRIPT_ARG]...

執行在主要模組中定義的伺服器

serve 命令使用主要模組的預設匯出,以決定要啟動哪些伺服器。

啟動在 server.ts 中定義的伺服器

deno serve server.ts

啟動在 server.ts 中定義的伺服器,監看變更並在 port 5050 上執行

deno serve --watch --port 5050 server.ts

類型檢查選項 Jump to heading

--check Jump to heading

啟用類型檢查。此子命令預設不執行類型檢查。如果提供 "all" 值,則會包含遠端模組。或者,可以使用 'deno check' 子命令。

--no-check Jump to heading

略過類型檢查。如果提供 "remote" 值,則會忽略來自遠端模組的診斷錯誤。

依賴管理選項 Jump to heading

--cached-only Jump to heading

要求遠端依賴已經被快取。

--frozen Jump to heading

如果鎖定檔過期則報錯。

--import-map Jump to heading

從本機檔案或遠端 URL 載入匯入地圖檔案。

--lock Jump to heading

檢查指定的鎖定檔。(如果未提供值,則預設為 "./deno.lock")。

--no-lock Jump to heading

停用自動探索鎖定檔。

--no-npm Jump to heading

不解析 npm 模組。

--no-remote Jump to heading

不解析遠端模組。

--node-modules-dir Jump to heading

設定 npm 套件的 node_modules 管理模式。

--reload Jump to heading

短旗標:-r

重新載入原始碼快取(重新編譯 TypeScript)無值 重新載入所有內容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals 重新載入特定模組 npm: 重新載入所有 npm 模組 npm:chalk 重新載入特定 npm 模組。

--vendor Jump to heading

切換本機 vendor 資料夾用於遠端模組,以及 node_modules 資料夾用於 npm 套件的使用。

選項 Jump to heading

--allow-scripts Jump to heading

允許為給定的套件執行 npm 生命周期腳本。注意:腳本僅在使用 node_modules 目錄 (--node-modules-dir) 時執行。

--cert Jump to heading

從 PEM 編碼檔案載入憑證授權單位。

--config Jump to heading

短旗標:-c

設定 deno 的不同方面,包括 TypeScript、程式碼檢查和程式碼格式化。通常,組態檔將被稱為 deno.jsondeno.jsonc 並自動偵測到;在這種情況下,此旗標不是必要的。

--env-file Jump to heading

從本機檔案載入環境變數。僅使用具有給定鍵的第一個環境變數。現有的程序環境變數不會被覆蓋,因此如果環境中已存在具有相同名稱的變數,則它們的值將被保留。如果您的 .env 檔案中存在同一環境變數的多個宣告,則應用遇到的第一個宣告。這由您作為參數傳遞的檔案順序決定。

--ext Jump to heading

設定所提供檔案的內容類型。

--host Jump to heading

要服務的 TCP 位址,預設為 0.0.0.0(所有介面)。

--location Jump to heading

一些 Web API 使用的 globalThis.location 的值。

--no-code-cache Jump to heading

停用 V8 程式碼快取功能。

--no-config Jump to heading

停用自動載入組態檔。

--parallel Jump to heading

平行執行多個伺服器工作執行緒。平行處理預設為可用的 CPU 數量或 DENO_JOBS 環境變數的值。

--port Jump to heading

要服務的 TCP 埠。傳遞 0 以選擇隨機可用埠 [預設值:8000]

--seed Jump to heading

設定隨機數字產生器種子。

--v8-flags Jump to heading

若要查看所有可用旗標的清單,請使用 --v8-flags=--help。旗標也可以透過 DENO_V8_FLAGS 環境變數設定。使用此旗標設定的任何旗標都會附加在 DENO_V8_FLAGS 環境變數之後。

除錯選項 Jump to heading

--inspect Jump to heading

在 host:port 上啟用檢查器 [預設值:127.0.0.1:9229]

--inspect-brk Jump to heading

在 host:port 上啟用檢查器,等待偵錯工具連線並在使用者腳本開始時中斷。

--inspect-wait Jump to heading

在 host:port 上啟用檢查器並等待偵錯工具連線,然後再執行使用者程式碼。

檔案監看選項 Jump to heading

--hmr Jump to heading

監看檔案變更並自動重新啟動程序。預設情況下,會監看進入點模組圖中的本機檔案。可以透過將其他路徑作為此旗標的引數傳遞來監看其他路徑。

--no-clear-screen Jump to heading

在監看模式下不要清除終端機畫面。

--watch Jump to heading

監看檔案變更並自動重新啟動程序。預設情況下,會監看進入點模組圖中的本機檔案。可以透過將其他路徑作為此旗標的引數傳遞來監看其他路徑。

--watch-exclude Jump to heading

從監看模式中排除提供的檔案/模式。

範例 Jump to heading

以下是如何使用宣告式 fetch 建立簡單 HTTP 伺服器的範例

server.ts
export default {
  async fetch(_req) {
    return new Response("Hello world!");
  },
};

然後您可以使用 deno serve 命令執行伺服器

deno serve server.ts

fetch 函數內部的邏輯可以自訂,以處理不同類型的請求並相應地提供內容

server.ts
export default {
  async fetch(request) {
    if (request.url.startsWith("/json")) {
      return Response.json({ hello: "world" });
    }

    return new Response("Hello world!");
  },
};

您找到需要的資訊了嗎?

隱私權政策