From d13b5933d03168b0f0b9b0221f50684d2eae7b5e Mon Sep 17 00:00:00 2001 From: bus Date: Sun, 18 Apr 2021 01:41:41 +0200 Subject: [PATCH] function to read from env --- api.js | 3 ++- env.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 env.js diff --git a/api.js b/api.js index 7caf844..17df1ac 100644 --- a/api.js +++ b/api.js @@ -1,9 +1,10 @@ const got = require('got'); +const { getEnvVar } = require('./env.js'); const fetchEvents = () => got('http://localhost:8384/rest/events', { headers: { - 'X-API-Key': process.env.API_KEY, + 'X-API-Key': getEnvVar('API_KEY','invalid') }, }).json(); diff --git a/env.js b/env.js new file mode 100644 index 0000000..8999dd4 --- /dev/null +++ b/env.js @@ -0,0 +1,9 @@ +module.exports = { + getEnvVar: function (varname, defaultvalue){ + var result = process.env[varname]; + if(result!=undefined) + return result; + else + return defaultvalue; + } +};