Jishnu
Jishnu

Reputation: 33

URL as environment variable in Node JS

I was making a micro service for HTTP requests (GET, POST, PUT, DELETE) in Node js and Cassandra. I was wondering if we can set the URL as an environment variable from another file and then pass it to make any specific request?

Upvotes: 2

Views: 7374

Answers (1)

Matt Kuhns
Matt Kuhns

Reputation: 1368

You can use a config variable in a config.js file like this:

var myConfig = {
   url: <your url>
}

module.exports = myConfig;

Then import it in another file:

const myConfig = require('./myConfig');

console.log(myConfig.url);

Environment variable are handle differently.

You kick off node with environmental variable:

URL=<MyURL> node index.js

Then you can reference the environmental variable like so:

console.log(process.env.URL)

Upvotes: 1

Related Questions