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

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 272
    Comment on it

    What exactly are the environment variables? Environment variables are a set of key value pairs on which our project configuration depend and which might change according to the environment in which code it running i.e development/production/stagging etc. As we don't want anyone to access of our environment variables due to security concerns that are attached with them like database credentials/amazon s3 credentials etc thus they are either setup before running our code in specified environment or we have a separate file which is not part of code which is created depending on environment and is loaded before any other code is executed.

     

    How exactly we can can setup environment variables in node?

     

    There are number of ways OS setting environment variable in node depending on your choice we will take each of them one at a time .

     

    1) The most common way of declaring environment variable is making entry of them in .bash_profile file before starting server for you project. If we take it in node way then we can export our environment variables before running out node server. Which can be done in the following way :


      

    export FB_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXX
    
    export FB_ACCESS_KEY_ID=XXXXXXXXXXXXXXXX

     

    In order to access these environment variables being set in  .bash_profile we will be using "process.env" . Here "process" behaves as a global object that can be accessed from anywhere and process.env is an object that contains the user environment.

     

    Thus now as "process" is a global object it can be accessed through out the node project , if you ever need to access any environment variables it can be accessed by

     

    var fbSecretAccessKey = process.env.FB_SECRET_ACCESS_KEY;

    var fbSecretAccessKey = process.env.FB_SECRET_ACCESS_KEY;

     

    The above approach is quite simple and make use of server and node inbuilt features. But it too have complications attached with it , problem start if you have multiple project running on the same instance of server. As common .bash_profile is being shared between different projects thus it will become difficult to manage key having same name beside this as .bash_profile increases in size it will also become difficult for you to remove any key as you wont be sure which project might be using it.

     

    Thus in order to overcome issues with this approach we need to have a better technique which loads environment variable specified to particular project only. We will be discussing the same in next approach.

     

 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: