How to implement configurable values instead of hard coding them?


#1

Hi
I have a requirement to have a configurable key value pair like app key for an API which can change for different instance of my website. Is there a way to have configurable values for a key which can be dynamically loaded in the server side js code?
Thanks


#2

One standard way to do this would be to use environment variables, i.e. define them in the shell script or similar that starts the server and read them in the code using process.env.XYZ.
Another option is to use Meteor.settings.


#3

I have created settings.json file and added the below
{
“apiappkey”: “ABCD5515-BD1B-4753-93AE-34328C81DDDD”
}

I am trying to see if I can access this value in server.js file using the below code
console.log(Meteor.settings.apiappkey);

But I get an exception as Undefined. Am I missing anything?


#4

the settings.json is under the same folder which has server.js


#5

You have to start meteor with the settings flag and pass in the path to that file

meteor --settings=settings.json


#6

i have done it but doesnt work still. This is the output. Is the equals important?

C:\Users\Varesh Rangan\AppData\Local.meteor\Rakshak>“C:\Users\Varesh Rangan\App
Data\Local.meteor\meteor.bat” run --settings server\settings.json
[[[[[ ~.meteor\Rakshak ]]]]]

=> Started proxy.
=> Started MongoDB.
I20150531-12:25:18.388(5.5)? ** You’ve set up some data subscriptions with Meteo
r.publish(), but
I20150531-12:25:18.390(5.5)? ** you still have autopublish turned on. Because au
topublish is still
I20150531-12:25:18.391(5.5)? ** on, your Meteor.publish() calls won’t have much
effect. All data
I20150531-12:25:18.391(5.5)? ** will still be sent to all clients.
I20150531-12:25:18.391(5.5)? **
I20150531-12:25:18.391(5.5)? ** Turn off autopublish by removing the autopublish
package:
I20150531-12:25:18.392(5.5)? **
I20150531-12:25:18.392(5.5)? ** $ meteor remove autopublish
I20150531-12:25:18.392(5.5)? **
I20150531-12:25:18.392(5.5)? ** … and make sure you have Meteor.publish() and M
eteor.subscribe() calls
=> Started your app.

=> App running at: http://localhost:3000/
Type Control-C twice to stop.

I20150531-12:25:18.392(5.5)? ** for each collection that you want clients to see
.
I20150531-12:25:18.392(5.5)?
I20150531-12:25:49.356(5.5)? FB225515-BD1B-4753-93AE-34328C81D0F0
I20150531-12:25:49.358(5.5)? hi
I20150531-12:26:43.815(5.5)? FB225515-BD1B-4753-93AE-34328C81D0F0
I20150531-12:26:43.822(5.5)? hi


#7

it worked. sorry I was looking at the wrong place.


#8

Good that it worked!

And no, that’s not important. meteor --settings settings.json will work as well!