Sails.js — Accessing local.js environment settings in controllers

后端 未结 1 1288
情歌与酒
情歌与酒 2021-02-01 10:17

In production I have AWS credentials stored as heroku config variables.

In development I want to include the config details in config/l

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 10:37

    Solution found. Step 3 was where I was having trouble.

    tl;dr

    What I didn't realize was that the module.exports.thing makes the thing object available through sails.config.thing. Good to know.


    1) I created a new file at config/aws.js with the contents

    // Get heroku config values     
    module.exports.aws = {
      key: process.env.AWS_KEY,
      secret: process.env.AWS_SECRET
    }
    

    2) In local.js put the actual AWS creds (this won't end up in the repository since sails automatically ignores local.js using gitignore).

    aws: {
      key: actual-key,
      secret: actual-secret
    }
    

    This allows for local testing where we don't have access to the heroku config settings, while protecting these values from being exposed in a github repo.

    3) Now, to access in the controller:

    var aws_config = sails.config.aws;
    
    AWS.config.update({
      accessKeyId: aws_config.key,
      secretAccessKey: aws_config.secret
    });
    

    0 讨论(0)
提交回复
热议问题