简体   繁体   English

如何将数据库环境变量添加到 Javascript

[英]How to add database environment variables to Javascript

I want to add my database environment variables to a .env file and use that file in my Javascript program in order to create a connection to my database using Node.js.我想将我的数据库环境变量添加到.env文件并在我的 Javascript 程序中使用该文件,以便使用 Node.js 创建到我的数据库的连接。

So here is my database info which I use to create connection with:所以这是我用来创建连接的数据库信息:

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "my password",
  database: "mydatabase"
});

Then I try to check if I am connected to my database using these lines:然后我尝试使用这些行检查我是否已连接到我的数据库:

(It should print "Connected!"). (它应该打印“已连接!”)。

con.connect(function(err) {   
  if (err) throw err;
  console.log("Connected!");
});

I want to put the first block of code in another file and require that file in my Node.js program.我想将第一个代码块放在另一个文件中,并require在我的 Node.js 程序中使用该文件。

How should I do this?我该怎么做? How should I require the file?我应该如何require文件? Thank you in advance :)先感谢您 :)

I suggest using dotenv package .我建议使用dotenv 包

Taken straight from the readme:直接取自自述文件:

  1. As early as possible in your application, require and configure dotenv.尽早在您的应用程序中,要求并配置 dotenv。

    require('dotenv').config()

  2. Create a .env file in the root directory of your project.在项目的根目录中创建一个.env文件。 Add environment-specific variables on new lines in the form of NAME=VALUE .NAME=VALUE的形式在新行上添加特定于环境的变量。 For example:例如:

    DB_HOST=localhost DB_USER=root DB_PASS=s1mpl3

  3. Usage ( process.env now has the keys and values you defined in your .env file.)用法( process.env现在具有您在.env文件中定义的键和值。)

    var db = require('db') db.connect({ host: process.env.DB_HOST, username: process.env.DB_USER, password: process.env.DB_PASS })

NOTE : Make sure your .gitignore has an entry to ignore .env files.注意:确保您的.gitignore有一个忽略.env文件的条目。

You can use a module called dotenv which will load an environments variables defined in a .env file or any file you specify.您可以使用名为dotenv的模块,该模块将加载.env文件或您指定的任何文件中定义的环境变量。 You would then load the .env in two ways:然后,您可以通过两种方式加载.env

  1. In the main script file, call require('dotenv').config()在主脚本文件中,调用require('dotenv').config()
  2. Via npm script: "dev": "node -r dotenv/config app.js"通过 npm 脚本: "dev": "node -r dotenv/config app.js"

Either works, but you do not want to commit .env to source control. .env都可以,但您不想将.env提交给源代码管理。 Always keep sensitive credentials out.始终保留敏感凭据。 You can create a an .env.example to alert new users the required variables.您可以创建一个.env.example来提醒新用户所需的变量。

using dotenv package.使用 dotenv 包。 Install dotenv package.安装 dotenv 包。

npm i dotenv npm 我 dotenv

Create a new .env file in project root directory.在项目根目录中创建一个新的 .env 文件。

touch .env触摸 .env


Add environment variables to .env file将环境变量添加到 .env 文件

API_HOST=HOST-PLACEHOLDER-URL
API_KEY=TOP-SECRET
APP_NAME=node_crud
APP_ENV=local
APP_KEY=base64:RIjL2Uw/Wdve+HJEvRNp6LHhzoHtLbplQcUp60CBIvs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=node_restapi
DB_USERNAME=root
DB_PASSWORD=
PORT = 5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM