【发布时间】:2018-10-23 15:48:59
【问题描述】:
我是一名原生 iOS Swift 开发人员,我曾对 Node.js 稍有涉足,所以这个过程对我来说有点陌生。
使用节点如果我想隐藏我的变量我首先将dotenv module、require 导入到我的app.js 文件中,设置.env 变量,将其添加到.gitignore 文件中,然后使用dotenv 模块来访问它们:
// on the cli
$ npm install dotenv --save
$ touch .env
$ touch .gitignore
// in the .env file
MY_SECRET_KEY=12345
// finally add the .env file to the .gitignore filel
// in the app.js file
const dotenv = require('dotenv');
dotenv.load();
initializeSomethingWith(process.env.MY_SECRET_KEY)
在 Heroku 中,我使用以下方法将密钥设置为秘密:
$ heroku config:set MY_SECRET_KEY=123456789
$ git push heroku master
我使用以下命令访问 heroku 配置变量:
process.env.MY_SECRET_KEY
我的困惑在于,一旦我需要 dotenv module,它如何区分 .env 文件中的内容与我为 heroku 变量设置的内容之间的区别?
app.js 文件:
const dotenv = require('dotenv');
dotenv.load();
var isThisAHerokuKey = process.env.MY_SECRET_KEY // key is 123456789
var orIsThisADotEnvKey = process.env.MY_SECRET_KEY // key is 12345
initializeSomethingWith(process.env.MY_SECRET_KEY) // which key is this using?
当然常识会说只是不要将键命名为相同的名称。
【问题讨论】: