Search code examples
mysqlnode.jsexpressnode-mysql

Configure Different Node MySQL connections for different environments


I have 3 different MySQL database connections depending the environment (local, dev and live).

Using node-mysql, how do I serve the correct connection details or detect the environment being used?


Solution

  • The process.env.NODE_ENV variable is where the value is stored.

    You can do something like:

    if (process.env.NODE_ENV == 'production') 
    

    OR this is built into the app.configure function

    app.configure('development', function(){
    
    });
    
    app.configure('production', function(){
    
    ...
    });
    

    SETTING THE VARIABLE FROM COMMAND LINE

    LINUX:

    $ echo export NODE_ENV=production >> ~/.bash_profile
    $ source ~/.bash_profile
    

    WINDOWS:

    set NODE_ENV=production
    setx NODE_ENV production