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

与世无争的帅哥 提交于 2019-12-02 18:34:46
Drew

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
});

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!