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

我只是一个虾纸丫 提交于 2019-12-03 05:17:40

问题


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

In development I want to include the config details in config/local.js, but how do I access the config details in a controller?

local.js contains:

module.exports = { aws_key: "...", aws_secret: "..." }

In my controller I have tried aws_key, config.aws_key, and others - but no luck. Is there a main app namespace that I can use to scope into the properties exported by local.js?

I am new to sails and I feel like this should be straight forward - any help would be appreciated.


回答1:


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



来源:https://stackoverflow.com/questions/21291111/sails-js-accessing-local-js-environment-settings-in-controllers

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