如何使用process.env将环境变量中的API密钥传递给Ember CLI? [英] How to pass API keys in environment variables to Ember CLI using process.env?

查看:399
本文介绍了如何使用process.env将环境变量中的API密钥传递给Ember CLI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将环境变量从bashrc传递给Ember CLI。我想象一种情况,你需要条纹api键或推送api键,你有他们在你的环境变量在bashrc。如何将api密钥传递给Ember CLI。



我尝试在 brocfile.js中使用Node.js process.env / code>和 environment.js ,但是当我尝试在Ember JS控制器中访问它时,属性为null。



在我添加的 environment.js 文件中,

  APP:{apiKey:process.env.KEY} 

在我的Ember JS控制器我试图访问它:

  import config from'.. /配置/环境'; 

并设置controller属性 lkey 如图所示以下,这不起作用:

  lkey:config.App.KEY 

接下来在我的 brocfile.js 中,我补充说: / p>

  var limaKey = process.env.Key; 
var app = new EmberApp({key:limaKey});

这仍然无效。

解决方案

我终于解决了这个问题。我面对两个选择。选项1是使用XHR从服务器端点获取api密钥。选项2是使用Nodejs process.env直接从环境变量获取api密钥。我更喜欢选项2,因为它使我不能做XHR请求。



您可以通过使用这个依赖于Nodejs Dotenv项目的ember-cli-addOn来获取选项2 >



在我的情况下,我选择做没有任何addOn。 / p>


  1. 首先将api-key添加到您的 .bashrc 中,如果您是Ubuntu或$ / $>


    $ b

     导出API_KEY = NwPyhL5 




    1. 重新加载 .bashrc 文件,所以你的设置被拿起来:





      source〜/ .bashrc 




    1. 在Ember中CLI在 config / environment.js 中的 ENV 对象中添加一个属性。默认看起来像这样





      module.exports = function (环境){
    var ENV = {
    modulePrefix:'rails-em-cli',
    environment:environment,
    baseURL:'/',
    locationType: 'auto',
    EmberENV:{

    }
    }

    现在到 ENV 对象,我们可以这样添加一个新属性 myApiKey

      module.exports = function(environment){
    var ENV = {
    modulePrefix:'rails-em- CLI,
    环境:环境,
    基本URL: '/',
    的locationType: '自动',
    myApikey:空,
    EmberENV:{

    }

    //将一个值赋给myApiKey

    if(environment ==='development'){
    // ENV.APP。 LOG_RESOLVER = true;

    ENV.myApiKey = process.env.API_KEY;
    }

    }

    请注意, env.API_KEY 正在获取我们添加到 .bashrc 并将其分配给 myApiKey 的设置。您将需要在您的服务器上安装Nodejs以使 process.env 正常工作。



    最后要访问控制器中的变量, / p>

      import config from'../config/environment'; 
    从ember导入Ember;

    导出默认值Ember.Controller.extend({

    yourKey:config.myApikey,

    });

    就是这样。


    How do I pass environment variables from bashrc to Ember CLI. I imagine a situation where you need stripe api keys or pusher api-keys and you have them in your environment variables in bashrc. How do you pass the api-keys to Ember CLI.

    I tried using Node.js process.env in both the brocfile.js and environment.js, but when I try to access it in the Ember JS controller, the property is null.

    In my environment.js file I added,

    APP: { apiKey: process.env.KEY }
    

    In My Ember JS controller I tried accessing it with:

    import config from  '../config/environment'; 
    

    And setting the controller property lkey as shown below, which didn't work:

    lkey: config.App.KEY
    

    Next in my brocfile.js, I added:

    var limaKey = process.env.Key; 
    var app = new EmberApp({key: limaKey});
    

    This still didn't work.

    解决方案

    I finally resolved this issue. I was faced with two options. Option 1 was to use XHR to fetch the api-keys from an end-point on the server. Option 2 is get the api-key directly from environment variables using Nodejs process.env. I prefer option 2 because it saves me from doing XHR request.

    You can get option 2 by using this ember-cli-addOn which depends on Nodejs Dotenv project

    In my case I choose to do it without any addOn.

    1. First add the api-key to your .bashrc if you are Ubuntu or the approapriate place for your own linux distro.

    export API_KEY=NwPyhL5
    

    1. Reload the .bashrc file, so your setting are picked up:

    source ~/.bashrc
    

    1. In Ember CLI add a property to the ENV object in config/environment.js. The default looks like this

    module.exports = function(environment) {
      var ENV = {
         modulePrefix: 'rails-em-cli',
         environment: environment,
         baseURL: '/',
         locationType: 'auto',
         EmberENV: {
    
          }
       }
    

    Now to that ENV object, we can add a new property myApiKey like this:

    module.exports = function(environment) {
      var ENV = {
        modulePrefix: 'rails-em-cli',
        environment: environment,
        baseURL: '/',
        locationType: 'auto',
        myApikey: null,
        EmberENV: {
    
         }
    
       //assign a value to the myApiKey
    
         if (environment === 'development') {
            // ENV.APP.LOG_RESOLVER = true;
    
            ENV.myApiKey = process.env.API_KEY;
          }              
    
       }
    

    Note that process.env.API_KEY is fetching the setting we added to .bashrc and assigning it to myApiKey. You will need to have Nodejs installed on your server for process.env to work.

    Finally to access that variable in your controller you do

    import config from '../config/environment';
    import Ember from 'ember';
    
    export default Ember.Controller.extend({
    
      yourKey: config.myApikey,
    
    });
    

    That's it.

    这篇关于如何使用process.env将环境变量中的API密钥传递给Ember CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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