4

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

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

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 ?

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

Community
  • 1
  • 1
Vinícius Fagundes
  • 1,983
  • 14
  • 24

2 Answers2

3

What I did to solve this issue:

Simplified 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:

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:

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

// using SendGrid's Node.js Library
var sendgrid = require("sendgrid")(config.sendgrid.api_key);
Vinícius Fagundes
  • 1,983
  • 14
  • 24
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;
hirra
  • 867
  • 7
  • 14