简体   繁体   中英

Environment variables in Azure WebApp with Node.js

I have successfully run a Node.js Webapp on Azure, which is connecting to an Azure SQL DB. Everything is working fine when entering the DB details manually into the code.

The issue comes when I try and use the connection details as environment variables.

My Node.js connection looks like this:

const config = {
user: `${APPSETTING_user}`,
password: `${APPSETTING_pw}`,
server: `${APPSETTING_host}`,
database: `${APPSETTING_db}`,
options: 
 {
  encrypt: true
 }
};

In the Azure Webapp, I have done the following:

  • Application Settings > App Settings

Entered the following:

  • Key: user, Value: {db user login}
  • Key: pw, Value: {my password}
  • Key: host, Value: {SQL server address}
  • Key: db, Value: {db name>}

Correct working values in plain text without curly brackets obviously.

I am getting an error on load that is cannot find db server: ${APPSETTING_host}

I have been looking through documentation and tutorials, which is limited or out of date on this - however from what I have read apparently I am doing it correctly?

You need to use process.env.ENV_VARIABLE to read environment variables in Node. So in your case, it would be process.env.user , process.env.pw , etc...

See Read environment variables in Node.js for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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