Sails.js - 访问控制器中的local.js环境设置 [英] Sails.js -- Accessing local.js environment settings in controllers

查看:101
本文介绍了Sails.js - 访问控制器中的local.js环境设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生产中,我将AWS凭据存储为heroku配置变量。

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

开发中我想要包括config / local.js中的配置详细信息,但如何访问控制器中的配置详细信息?

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包含:

local.js contains:


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

在我的控制器中,我尝试过 aws_key config.aws_key 和其他人 - 但没有运气。是否有一个主要的应用程序命名空间,我可以用它来限制local.js导出的属性?

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?

我是sails的新手,我觉得这应该是直截了当的 - 任何帮助将不胜感激。

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

推荐答案

找到解决方案。第3步是我遇到麻烦的地方。

什么我没意识到 module.exports.thing 通过<使 thing 对象可用code> sails.config.thing 。很高兴知道。

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

1)我在config / aws.js创建了一个新文件with the contents

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)在local.js中放置实际的AWS信用卡(这不会在存储库中结束,因为风帆会自动忽略使用gitignore的local.js)。

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
}

这允许我们无法访问heroku配置设置的本地测试,同时保护这些值不会暴露在github仓库中。

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)现在,要在控制器中访问:

3) Now, to access in the controller:

var aws_config = sails.config.aws;

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

这篇关于Sails.js - 访问控制器中的local.js环境设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆