Where should I store secret strings on Node server?

后端 未结 4 1362
臣服心动
臣服心动 2021-02-05 12:29

Well, I\'ve come with a problem. How can I store passwords, db url and important strings that should not go to my public version control?

I\'ve come up with 3 solutions.

4条回答
  •  暖寄归人
    2021-02-05 12:48

    The dotenv package can be used to load configuration and secrets from a .env file into process.env. For production, the .env file doesn't have to exist.

    Example:

    require('dotenv').config();
    
    const oauth2 = require('simple-oauth2').create({
      client: {
        id: process.env.TWITTER_CONSUMER_KEY,
        secret: process.env.TWITTER_CONSUMER_SECRET
      }
    });
    

    .env file:

    TWITTER_CONSUMER_KEY=bMm...
    TWITTER_CONSUMER_SECRET=jQ39...
    

    .gitignore:

    .env
    

提交回复
热议问题