简体   繁体   中英

Different env variables for testing and development in nodejs

I'm developing a nodejs api in my free time, and I'm trying to implement testing now. I'm currently loading my environment variables from a.env file (loaded using dotenv ), which include the DB_URI , DB_USER and DB_PASSWORD for my development mongodb database.

Now, I would like to create a separate database for testing however I don't know how would I load different variables to connect to the testing database instead of the development database. I deploy to Heroku where I have different environment variables so that's covered just fine.

I've tried to find online for some answers for best practices, but I have been unable to. I thought of creating a different.env file, however that's not recommended according to the documentation on npmjs. .

Other resources recommended recommended hard coding the specific variables I needed in the package.json script. However, the script would be huge if I had to change all the variables needed to connect to a different database.

Can I get some help understanding how I should do this?

Thanks!

PS: In case it's needed, I'm using mocha and supertest for my tests.

  1. In your .env file, add variables for each environment:

     DB_URI_DEVELOPMENT="https://someuri.com" DB_USER_DEVELOPMENT=someuser DB_PASSWORD_DEVELOPMENT=somepassword DB_URI_TEST="https://otheruri.com" DB_USER_TEST=otheruser DB_PASSWORD_TEST=otherpassword


  1. Start the application in development :

     NODE_ENV=development node server.js

    or in test :

     NODE_ENV=test node server.js


  1. Access the environment variables for this environment:

     // don't try to load .env file on Heroku if (process.env.NODE_ENV !== 'production') { require('dotenv').config() } // get the current environment var env = process.env.NODE_ENV // convert to uppercase var envString = env.toUpperCase() // access the environment variables for this environment var dbUri = process.env['DB_URI_' + envString] var dbUser = process.env['DB_USER_' + envString] var dbPassword = process.env['DB_PASSWORD_' + envString]


Hope that helps!

Personally I like having the same environment variable names for different environments, so I created a env.ts to include where I want to load my dotEnv so I can use the same environment variable names throughout my application.

import * as process from "process";
import * as dotenv from 'dotenv'
import * as path from "path";

const getDotEnvPath = (env?: string) => {
    if (env == 'TEST') {
        return '.env.test'
    }
    return '.env'
}
dotenv.config({path: path.resolve(process.cwd(), getDotEnvPath(process.env.NODE_ENV?.toUpperCase()))})

Its also good to note that by default dotEnv only sets the environment variables when they are not already set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM