简体   繁体   中英

Retrieve Azure environment variables in NodeJs

I have an app service set up in Azure with a static built NodeJS web application. The application runs fine and I want to move the api key and hostname of the api to variables in the environment.

In Azure->Settings->Configuration->Application Settings I have added two environment variables and set a value for each.

In my Constants.js file I have

export const token = process.env.API_KEY;
export const host = process.env.API_HOST;

But process.env.API_KEY is always null.

I have even tried :

export const token = process.env["API_KEY"];

To no avail. What am I doing wrong?

Both of the code you provided all work on my side.

export const token = process.env.API_KEY;
export const token = process.env["API_KEY"];

Try to redeploy your website and view it in another browser.

在此处输入图片说明

Update:

const http = require('http');

//var APPINSIGHTS_INSTRUMENTATIONKEY= process.env.APPINSIGHTS_INSTRUMENTATIONKEY;

const server = http.createServer((request, response) => {
    var a,b,c;
    if(process.env.NODE_ENV === "production")
    {
        a= process.env.NODE_ENV ;
        b= process.env.APPINSIGHTS_INSTRUMENTATIONKEY;
        c= process.env.KEY_VALUE;
    }
    else if(process.env.NODE_ENV === "development")
    {
        a="Welcome";
        b="testparams";
        c="testvalue";
    }
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end(a+"  && "+b+"myCustomKey="+c);
});

const port = process.env.PORT || 1337;
server.listen(port);

console.log("Server running at http://localhost:%d", port);

I ran into this same issue and had the same env vars (mentioned in a follow up answer by the author) showing then finally found that the scripts used by create-react-app specifically expose those vars plus any beginning with REACT_APP_ .

I tweaked the logic to allow for additional env vars to pass through, or you could rename you're variables to use the above prefix.

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