# 访问配置值

> Source: https://strapi.nodejs.cn/cms/configurations/guides/access-configuration-values

🌐 How to access to configuration values from the code

使用 `strapi.config.get()` 方法访问启动时加载的配置值，并对所有配置文件中的嵌套键使用点符号表示法。

🌐 Access configuration values loaded on startup using the `strapi.config.get()` method with dot notation for nested keys across all configuration files.

所有的[配置文件](/cms/configurations)都会在启动时加载，并可以通过 `strapi.config` 配置提供程序访问。

🌐 All the [configuration files](/cms/configurations) are loaded on startup and can be accessed through the `strapi.config` configuration provider.

如果 `/config/server.ts|js` 文件具有以下配置：

🌐 If the `/config/server.ts|js` file has the following configuration:

```js
  module.exports = {
    host: '0.0.0.0',
  };
  ```

```ts
  export default {
    host: '0.0.0.0',
  };
  ```

那么可以这样访问 `server.host` 键:

🌐 then the `server.host` key can be accessed as:

  ```js
  strapi.config.get('server.host', 'defaultValueIfUndefined');
  ```

嵌套键可以通过 [dot notation](https://web.nodejs.cn/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#dot_notation)访问。

:::note

文件名用作访问配置的前缀。

🌐 The filename is used as a prefix to access the configurations.

:::

配置文件可以是 `.js`、`.ts` 或 `.json` 文件。

🌐 Configuration files can either be `.js`, `.ts`, or `.json` files.

使用 `.js` 或 `.ts` 文件时，可以导出配置：

🌐 When using a `.js` or `.ts` file, the configuration can be exported:

- 作为一个对象：

  ```js
  module.exports = {
    mySecret: 'someValue',
  };
  ```

  ```ts
  export default {
    mySecret: 'someValue',
  };
  ```

- 或者作为返回配置对象的函数（推荐用法）。该函数将可以访问[`env` 工具](/cms/configurations/guides/access-cast-environment-variables)：

  ```js
  module.exports = ({ env }) => {
    return {
      mySecret: env('MY_SECRET_KEY', 'defaultSecretValue'),
    };
  };
  ```

  ```ts
  export default ({ env }) => {
    return {
      mySecret: env('MY_SECRET_KEY', 'defaultSecretValue'),
    };
  };
  ```
