I don't like the idea of sprinkling process.env all over the place and wanted to abstract it out a bit. I'm new to Node and know there must be a better way to do a few things.
- Not repeat the name 4 times (twice as a string and twice as a field)
- Any cleaner way to create properties and centralize existence checks than how I've done it? Mind you, I am trying to keep this Node 4.3.2 compatible sans Babel transpilers to keep it simple and working in AWS Lambda's NodeJs environment.
// module
module.exports = {
}
var configError = function (keyName) {
console.error("ExternalVars error! Could not load a value for: "+keyName);
process.exit(1);
}
// TODO: extract repeated function?
Object.defineProperty(module.exports, 'SAMPLE_SECRET', {
get: function() {
if (!process.env.SAMPLE_SECRET)
configError('SAMPLE_SECRET');
else
return process.env.SAMPLE_SECRET;
}
});
Object.defineProperty(module.exports, 'SAMPLE_SECRET_MORE', {
get: function() {
if (!process.env.SAMPLE_SECRET_MORE)
configError('SAMPLE_SECRET_MORE');
else
return process.env.SAMPLE_SECRET_MORE;
}
});
// usage example
var externalvars = require('./config/externalvars.js');
var sampleSecret = externalvars.SAMPLE_SECRET;
var sampleSecretMore = externalvars.SAMPLE_SECRET_MORE;