使用angular-fullstack生成器时,访问后端envvariables的最佳方法是什么?
我正在使用Yeoman的angular度全叠层发生器。
我已经更新了我的服务器/ config / environment / local.env.js文件:
module.exports = { DOMAIN: 'http://localhost:9000', SESSION_SECRET: 'vfsite2-secret', SENDGRID : { API_KEY : 'my_api_key' }, DEBUG: '' };
如何最好的方式,我可以使用SENDGRID.API_KEY
其他在我的服务器上的文件,例如我的server/api/thing/thing.controller.js
?
注意这个问题不是重复的问题,因为我想在服务器端使用。
我做了什么来解决这个问题:
简化的server/config/environment/local.env.js
:
module.exports = { DOMAIN: 'http://localhost:9000', SESSION_SECRET: 'vfsite2-secret', SENDGRID_API_KEY: 'my_api_key', DEBUG: '' };
更新我的configuration文件server/config/environment/index.js
:
var all = { env: process.env.NODE_ENV, // ... other configs here // SendGrid connection options sendgrid: { 'api_key': process.env.SENDGRID_API_KEY } };
在我的控制器文件server/api/thing/thing.controller.js
重新执行我的server/api/thing/thing.controller.js
:
import config from '../../config/environment'; // using SendGrid's Node.js Library var sendgrid = require("sendgrid")(config.sendgrid.api_key);
你的意思是全球对象?
global.globalConfig = { DOMAIN: 'http://localhost:9000', SESSION_SECRET: 'vfsite2-secret', SENDGRID : { API_KEY : 'my_api_key' }, DEBUG: '' }; module.exports = global.globalConfig;