Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Environment Variables (Part 2)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    In part 1 of this series we have seen how easily we can export environment variables to .bash_profile file with Linux "export" command and then using it with "process.env" . But this approach do have some drawbacks does not scale well . To overcome the drawbacks of earlier approach we will look into an approach where we can maintain environment variables in some persistent storage in some well-defined format which is easy to maintain and scale as per environment.

     

    Let's create a file names application.json which will be placed in the root folder of the project and will be added on a server as needed , won't be part of code packet. The standard format of application.json will be something like this :

    {
      "development": {
          "S3_app_key": "XXXXXXXXXXXXXXXXX",
          "S3_app_secret": "XXXXXXXXXXXXXXXXX",
      },
      "production": {
          "S3_app_key": "XXXXXXXXXXXXXXXXX",
          "S3_app_secret": "XXXXXXXXXXXXXXXXX",
      }
    }

     

    The above format shows how we can store same settings for a different environment and then we can extend the file as needed as per different server you have i.e production/development/staging.

     

    Now as we have application.json file ready which will act as our environment variables container we need some approach so as to include this file in our code-base.

     

    Loading JSON file in a node is quite easy what you simply need to do is require particular JSON file and a node will automatically load the same for you and parse it too thus JSON object being received after requiring the file is ready to use.

     

    Let's create a file of name environment.js which will load this application.json file for us , although we can do this directly in a file where it is needed but we would like to abstract it in a function so that we can use it in project anywhere just by calling it . File environment.js would look something like this :

    
    
        var application = require('application.json');
    
        exports.config = function() {
          var node_env = process.env.NODE_ENV || 'development';
          return env[node_env];
        };
    

     

    Once environment.js is ready we can use it in our code-base as following by including it in app.js and using an object property.

        var environment = require('./routes/environment')
        var config = environment.config();
    
        var S3_app_key = config.S3_app_key;
    

     

    Thus using the above approach we can have values for environment variable as per environment and thus now mashing it up with different environment scope. Although this approach is good enough to be used but some developer prefer to use some pre-built modules which can help us with environment variables. Thus continuing forward I will let you know about few node modules which can be used to achieve same.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: