
# 开发

> 仅影响开发服务器（`nitro dev`）的选项。

::note
本页所有示例都假设已在 `nitro.config.ts` 中导入以下内容：

```ts
import { defineConfig } from "nitro";
```
::

### `devServer`

- 默认值：`{ watch: [] }`{lang=ts}

开发服务器选项。可以使用 `watch`，在指定路径中的文件发生变化时重新加载开发服务器。

支持 `port`、`hostname`、`watch`、`runner`、`drain` 和 `devtoolsJson` 选项。

```ts
export default defineConfig({
  devServer: {
    port: 3001,
    watch: ["./server/plugins"],
  },
});
```

开发服务器默认会提供 `/.well-known/appspecific/com.chrome.devtools.json`，以便 Chrome DevTools 将项目目录连接为[自动工作区](https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md)，并将编辑内容保存回源文件。工作区 UUID 会保存在 `buildDir` 中。设置 `devServer.devtoolsJson: false` 可禁用此功能。

默认情况下，重新加载时会先关闭上一个开发 worker：新请求会等待它退出，超过 5 秒仍未结束的请求、流式响应和 WebSocket 会被中断。设置 `devServer.drain: true` 可让它继续处理这些连接，同时由新 worker 接收新请求。旧 worker 会在连接结束后关闭，最迟等待 60 秒（使用 `drain: { timeout: 600_000 }` 设置自定义上限，使用 `Infinity` 表示不限时）。

### `watchOptions`

开发模式的文件监听选项。更多信息请参阅 [chokidar](https://github.com/paulmillr/chokidar)。

```ts
export default defineConfig({
  watchOptions: {
    ignored: ["**/node_modules/**", "**/dist/**"],
  },
});
```

### `devProxy`

开发服务器的代理配置。

可以使用此选项覆盖开发服务器路由并转发请求。

```ts
export default defineConfig({
  devProxy: {
    "/proxy/test": "http://localhost:3001",
    "/proxy/example": { target: "https://example.com", changeOrigin: true },
    "/proxy/socket/**": { target: "http://localhost:3002", ws: true },
  },
});
```

路由的匹配方式与其他 Nitro 路由相同：`/proxy/test` 只匹配该确切路径；使用 `/proxy/test/**` 也可代理子路径。设置 `ws: true` 还可代理 WebSocket 升级请求。

所有可用的目标选项请参阅 [httpxy](https://github.com/unjs/httpxy)。

::note
开发服务器运行在 Bun 或 Deno 上时（例如 `bun --bun nitro dev`），会使用该运行时的原生服务器监听。HTTP 请求通过 httpxy 的 `proxyFetch` 代理，支持 `target`、`headers`、`auth`、`xfwd`、`changeOrigin`、`secure`、`ssl`、`agent`、`prependPath`、`ignorePath`、`proxyTimeout` 和 `followRedirects`。WebSocket 通过 [crossws](https://crossws.h3.dev) 代理，支持 `target`、`headers`、`auth`、`xfwd`、`prependPath` 和 `ignorePath`（上游 `Host` 响应头始终与目标一致）。只有在 Node.js 上，才能向 `miniflare` 运行器（Cloudflare 预设）的开发 worker 发起 WebSocket 升级请求。
::
