【问题标题】:Sails database configuration through environment variables通过环境变量配置 Sails 数据库
【发布时间】:2016-08-24 15:31:19
【问题描述】:

我曾经在 config/env/development.js 和 config/env/production.js 中配置 Sails 到我的数据库的连接:

module.exports = {
   connections: {
      'postgres': {
         host: 'localhost',
         user: 'myUser',
         password: 'myPassword',
         database: 'myDatabase'
      }
   }
};

如果我想按照here 的说明用环境变量替换我的环境配置文件怎么办?

我希望使用这些变量,但它不起作用:

  • sails__connections_postgres_host
  • sails__connections_postgres_user
  • sails__connections_postgres_password
  • sails__connections_postgres_database

【问题讨论】:

  • 试试看怎么样?

标签: configuration sails.js environment-variables configuration-files


【解决方案1】:

你的下划线是倒着的。查看您链接的文档,您的变量应如下所示:

sails_connections__postgres__host

等等。 ('sails' 后一个下划线,之后每个键之间有两个下划线)。

此外,值得注意的是,您可以在代码中引用环境变量,因此可以选择在 config/env/development.js(或生产环境)中引用环境变量“dbHostname”:

module.exports = {
    connections: {
        'postgres': {
            host: process.env.dbHostname,
            user: process.env.dbUser,
            password: process.env.dbPassword,
            database: process.env.db
        }
    }
}

然后在提升服务器时创建这些环境变量,即

dbHostname="http://something" dbUser="Your_User" dbPassword="password" db="database" sails lift

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2016-12-26
    • 2022-08-13
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多