deno serve
命令行用法
o serve [选项] [脚本参数]...
运行主模块中定义的服务器
serve 命令使用主模块的默认导出来确定要启动的服务器。
启动 server.ts 中定义的服务器:
deno serve server.ts
启动 server.ts 中定义的服务器,监听更改并在端口 5050 上运行:
deno serve --watch --port 5050 server.ts
了解更多: https://docs.deno.com/go/serve
不稳定选项 Jump to heading
--unstable
Jump to heading
--unstable
标志已被弃用。请改用细粒度的 --unstable-*
标志.
--unstable-bare-node-builtins
Jump to heading
启用不稳定的 bare node builtins 功能.
--unstable-bbroadcast-channel
Jump to heading
启用不稳定的 BroadcastChannel
API.
--unstable-cron
Jump to heading
启用不稳定的 Deno.cron API.
--unstable-detect-cjs
Jump to heading
在更多情况下将模棱两可的 .js、.jsx、.ts、.tsx 文件视为 CommonJS 模块.
--unstable-kv
Jump to heading
启用不稳定的键值存储 API.
--unstable-net
Jump to heading
启用不稳定的网络 API.
--unstable-node-globals
Jump to heading
在所有地方暴露 Node 全局变量.
--unstable-sloppy-imports
Jump to heading
启用通过扩展探测、.js 到 .ts 的转换以及目录探测来解析模块的不稳定功能.
--unstable-temporal
Jump to heading
启用不稳定的 Temporal API.
--unstable-unsafe-proto
Jump to heading
启用不安全的 proto 支持。这是一个安全风险。.
--unstable-webgpu
Jump to heading
启用不稳定的 WebGPU
API.
--unstable-worker-options
Jump to heading
启用不稳定的 Web Worker API.
类型检查选项 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 加载导入映射文件 文档:https://docs.deno.com/runtime/manual/basics/import_maps.
--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 模块管理模式.
--reload
Jump to heading
Short flag: -r
重新加载源代码缓存(重新编译 TypeScript) 无值 重新加载所有内容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals 重新加载特定模块 npm: 重新加载所有 npm 模块 npm:chalk 重新加载特定的 npm 模块.
--vendor
Jump to heading
切换远程模块的本地 vendor 文件夹和 npm 包的 node_modules 文件夹的使用.
Options Jump to heading
--allow-scripts
Jump to heading
允许运行给定包的 npm 生命周期脚本
注意:只有在使用 node_modules 目录时才会执行脚本(--node-modules-dir
).
--cert
Jump to heading
从 PEM 编码文件加载证书颁发机构.
--config
Jump to heading
Short flag: -c
配置 deno 的不同方面,包括 TypeScript、代码检查和代码格式化
通常配置文件会被称为 deno.json
或 deno.jsonc
并自动检测;在这种情况下,此标志不是必需的。
文档:https://docs.deno.com/go/config.
--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 服务器:
export default {
async fetch(_req) {
return new Response("Hello world!");
},
};
然后,你可以使用 deno serve
命令运行服务器:
deno serve server.ts
fetch
函数中的逻辑可以自定义,以处理不同类型的请求并相应地提供内容:
export default {
async fetch(request) {
if (request.url.startsWith("/json")) {
return Response.json({ hello: "world" });
}
return new Response("Hello world!");
},
};