> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Interface: Server

Server 选项用于改变 dev-server 的行为。

## Properties

### base?

> `optional` **base?**: `string`

配置服务器的基础路径。

#### Default Value

`'/'`

#### Remarks

默认情况下，用户可以通过 `http://<host>:<port>/main.lynx.bundle` 访问 lynx bundle。

如果希望通过 `http://<host>:<port>/foo/main.lynx.bundle` 访问 lynx bundle，可以将 `server.base` 修改为 `/foo`。

更多信息可参考 [server.base](https://rsbuild.rs/config/server/base)。

#### Example

```js
import { defineConfig } from '@lynx-js/rspeedy'
export default defineConfig({
  server: {
    base: '/dist'
  },
})
```

***

### compress?

> `optional` **compress?**: `boolean` | `CompressOptions`

配置是否对开发服务器或预览服务器提供的静态资源启用 [gzip compression](https://developer.mozilla.org/en-US/docs/Glossary/gzip_compression)。

#### Default Value

```ts
true
```

#### Remarks

详见 [Rsbuild - server.compress](https://rsbuild.rs/config/server/compress)。

#### Examples

如需禁用 gzip 压缩，可将 compress 设置为 false：

```js
export default {
  server: {
    compress: false,
  },
}
```

以 /foo 开头时进行压缩

```js
export default {
  server: {
    compress: {
      filter: (req) => {
        if (req.url?.includes('/foo')) {
          return false;
        }
        return true;
      },
    },
  },
}
```

设置 zlib 压缩级别

```js
export default {
  server: {
    compress: {
      level: 6,
    },
  },
}
```

***

### cors?

> `optional` **cors?**: `boolean` | `CorsOptions`

为开发服务器或预览服务器配置 CORS。

#### Default Value

```ts
Uses Rsbuild's default CORS options.
```

#### Remarks

- 设置为对象时，使用指定的选项启用 CORS。

- 设置为 `true` 时，使用默认选项启用 CORS（允许所有来源，不推荐）。

- 设置为 `false` 时，禁用 CORS。

详见 [Rsbuild - server.cors](https://rsbuild.rs/config/server/cors)。

#### Example

```js
export default {
  server: {
    cors: {
      origin: 'https://example.com',
    },
  },
}
```

***

### headers?

> `optional` **headers?**: `Record`\<`string`, `string` | `string`\[]>

为所有响应添加响应头。

#### Default Value

```ts
undefined
```

#### Example

```js
import { defineConfig } from '@lynx-js/rspeedy'
export default defineConfig({
  server: {
    headers: {
      'Access-Control-Allow-Origin': '**',
    },
  },
})
```

***

### host?

> `optional` **host?**: `string`

**`Lynx Default Changed`**

指定 Rspeedy Server 监听的 host。

#### Default Value

```ts
"0.0.0.0"
```

#### Remarks

在 `rspeedy dev` 期间，如果未设置 `server.host`，dev 插件会让服务器保持绑定在 IPv4 通配地址上，同时使用检测到的非回环 IPv4 地址（例如 `192.168.1.50`）来解析 dev server 相关的 URL 和客户端 host 设置。如果没有可用的 IPv4 地址，会绑定到 IPv6 通配地址，并使用检测到的 IPv6 地址生成 URL。如果没有可用的非回环 IP，则会回退到 IPv4 回环地址的 URL。如果你有多个网络接口，请显式设置 `server.host` 来选择所需的地址。

#### Example

将 host 设置为自定义值：

```js
import { defineConfig } from '@lynx-js/rspeedy'
export default defineConfig({
  server: {
    host: "192.168.1.50",
  },
})
```

***

### port?

> `optional` **port?**: `number`

指定 Rspeedy Server 监听的端口。

#### Default Value

Rsbuild 默认将该选项设置为 `3000`。

#### Remarks

默认情况下，当配置的端口被占用时，服务器会自动递增端口号。

#### Example

将端口设置为自定义值：

```js
import { defineConfig } from '@lynx-js/rspeedy'
export default defineConfig({
  server: {
    port: 3470,
  },
})
```

***

### proxy?

> `optional` **proxy?**: `ProxyConfig`

为开发服务器或预览服务器配置代理规则，将请求代理到指定的服务。

#### Default Value

```ts
undefined
```

#### Example

```js
export default {
  server: {
    proxy: {
      // http://localhost:3000/api -> http://localhost:3000/api
      // http://localhost:3000/api/foo -> http://localhost:3000/api/foo
      '/api': 'http://localhost:3000',
    },
  },
}
```

***

### strictPort?

> `optional` **strictPort?**: `boolean`

当端口被占用时，Rspeedy 会自动递增端口号，直到找到可用端口。

#### Default Value

```ts
false
```

#### Remarks

默认情况下，严格端口模式处于禁用状态。将 strictPort 设置为 true 后，如果端口被占用，Rspeedy 会抛出异常。
