使用angular-fullstack生成器时,在后端访问env变量的最佳方法是什么?

时间:2021-06-28 23:12:32

I'm using Yeoman's angular-fullstack generator.

我正在使用Yeoman的angular-fullstack发生器。

And I have updated my server/config/environment/local.env.js file:

我已经更新了我的server / config / environment / local.env.js文件:

module.exports = {
  DOMAIN: 'http://localhost:9000',
  SESSION_SECRET: 'vfsite2-secret',
  SENDGRID : {
      API_KEY : 'my_api_key'
  },
  DEBUG: ''
};

How the best way can I use SENDGRID.API_KEY else where on my server files, for instance on my server/api/thing/thing.controller.js ?

我最好的方法是如何在我的服务器文件上使用SENDGRID.API_KEY,例如在我的服务器/ api / thing / thing.controller.js上?

Notice this is not a duplicated question to this similar question, because I want to use on server-side.

请注意,对于这个类似的问题,这不是一个重复的问题,因为我想在服务器端使用。

2 个解决方案

#1


3  

What I did to solve this issue:

我做了什么来解决这个问题:

Simplified server/config/environment/local.env.js:

简化的server / config / environment / local.env.js:

module.exports = {
    DOMAIN: 'http://localhost:9000',
    SESSION_SECRET: 'vfsite2-secret',
    SENDGRID_API_KEY: 'my_api_key',
    DEBUG: ''
};

Updated my config file server/config/environment/index.js:

更新了我的配置文件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
  }
};

Retrived my sendgrid api_key in my controller file server/api/thing/thing.controller.js:

在我的控制器文件server / api / thing / thing.controller.js中重新发送了我的sendgrid api_key:

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

// using SendGrid's Node.js Library
var sendgrid = require("sendgrid")(config.sendgrid.api_key);

#2


2  

You mean global object?

你的意思是全球对象?

global.globalConfig = {
  DOMAIN: 'http://localhost:9000',
  SESSION_SECRET: 'vfsite2-secret',
  SENDGRID : {
      API_KEY : 'my_api_key'
  },
  DEBUG: ''
};

module.exports = global.globalConfig;

#1


3  

What I did to solve this issue:

我做了什么来解决这个问题:

Simplified server/config/environment/local.env.js:

简化的server / config / environment / local.env.js:

module.exports = {
    DOMAIN: 'http://localhost:9000',
    SESSION_SECRET: 'vfsite2-secret',
    SENDGRID_API_KEY: 'my_api_key',
    DEBUG: ''
};

Updated my config file server/config/environment/index.js:

更新了我的配置文件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
  }
};

Retrived my sendgrid api_key in my controller file server/api/thing/thing.controller.js:

在我的控制器文件server / api / thing / thing.controller.js中重新发送了我的sendgrid api_key:

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

// using SendGrid's Node.js Library
var sendgrid = require("sendgrid")(config.sendgrid.api_key);

#2


2  

You mean global object?

你的意思是全球对象?

global.globalConfig = {
  DOMAIN: 'http://localhost:9000',
  SESSION_SECRET: 'vfsite2-secret',
  SENDGRID : {
      API_KEY : 'my_api_key'
  },
  DEBUG: ''
};

module.exports = global.globalConfig;