Guide

配置


¥Configuration

自定义并扩展 Nitro 默认值。

有关可用选项,请参阅 config reference

你可以使用配置文件自定义 Nitro 构建器。

¥You can customize your Nitro builder with a configuration file.

export default defineNitroConfig({
  // Nitro options
})
如果你正在使用 Nuxt,请在 Nuxt 配置中使用 nitro 选项。
Nitro 使用 unjs/c12 加载配置,从而提供更多可能性,例如在当前工作目录或用户主目录中使用 .nitrorc 文件。

运行时配置

¥Runtime configuration

Nitro 提供了一个运行时配置 API,用于在应用中公开配置,并能够通过设置环境变量在运行时更新配置。当你想要为不同环境(例如开发、预发布和生产)公开不同的配置值时,这很有用。例如,你可以使用它为不同的环境公开不同的 API 端点或公开不同的功能开关。

¥Nitro provides a runtime config API to expose configuration within your application, with the ability to update it at runtime by setting environment variables. This is useful when you want to expose different configuration values for different environments (e.g. development, staging, production). For example, you can use this to expose different API endpoints for different environments or to expose different feature flags.

首先,你需要在配置文件中定义运行时配置。

¥First, you need to define the runtime config in your configuration file.

export default defineNitroConfig({
  runtimeConfig: {
    apiToken: "dev_token", // `dev_token` is the default value
  }
})

你现在可以使用 useRuntimeConfig(event) 访问运行时配置。在事件处理程序和工具中使用 useRuntimeConfig(event),并避免在环境全局上下文中调用它。这可能会导致意外行为,例如在不同的请求之间共享相同的运行时配置。

¥You can now access the runtime config using useRuntimeConfig(event). Use useRuntimeConfig(event) within event handlers and utilities and avoid calling it in ambient global contexts. This could lead to unexpected behavior such as sharing the same runtime config across different requests.

server/api/example.get.ts
export default defineEventHandler((event) => {
  return useRuntimeConfig(event).apiToken // Returns `dev_token`
});

本地开发

¥Local development

最后,你可以使用环境变量更新运行时配置。你可以在开发环境中使用 .env 文件,并在生产环境中使用平台变量(见下文)。

¥Finally, you can update the runtime config using environment variables. You can use a .env file in development and use platform variables in production (see below).

在项目根目录中创建一个 .env 文件:

¥Create an .env file in your project root:

.env
NITRO_API_TOKEN="123"

重新启动开发服务器,获取 /api/example 端点,你应该会看到响应是 123 而不是 dev_token

¥Re-start the development server, fetch the /api/example endpoint and you should see 123 as the response instead of dev_token.

请记住,你仍然可以使用 import.meta.envprocess.env 全局访问环境变量,但请避免在环境全局上下文中使用它们,以防止出现意外行为。

¥Do not forget that you can still universally access environment variables using import.meta.env or process.env but avoid using them in ambiant global contexts to prevent unexpected behavior.

生产环境

¥Production

你可以在生产环境中定义变量来更新运行时配置。所有变量必须以 NITRO_ 为前缀才能应用于运行时配置。它们将覆盖 nitro.config.ts 文件中定义的运行时配置变量。

¥You can define variables in your production environment to update the runtime config. All variables must be prefixed with NITRO_ to be applied to the runtime config. They will override the runtime config variables defined within your nitro.config.ts file.

NITRO_API_TOKEN="123"

在运行时配置中,请使用驼峰式命名法定义键。在环境变量中,使用蛇形命名法 (snake_case) 和大写字母定义键。

¥In runtime config, define key using camelCase. In environment variables, define key using snake_case and uppercase.

{
  helloWorld: "foo"
}
NITRO_HELLO_WORLD="foo"