4

I am trying to use npm module fb to use facebook api. The config file is located in the module and here is the snapshot of the same

var config = { };

// should end in /
config.rootUrl  = process.env.ROOT_URL                  || 'http://localhost:3000/';


config.facebook = {
    appId:          process.env.FACEBOOK_APPID          || '130243393813697',
    appSecret:      process.env.FACEBOOK_APPSECRET      || 'c82696768ae4ad8b63db874cb64eb558',
    appNamespace:   process.env.FACEBOOK_APPNAMESPACE   || 'nodescrumptious',
    redirectUri:    process.env.FACEBOOK_REDIRECTURI    ||  config.rootUrl + 'login/callback'
};

module.exports = config;

I don't wish to change the config file of the module since node_modules folder is kept in the gitignore list. For configuring the module to use my app's appId and appSecret, I need to set the process.env variables FACEBOOK_APPID and FACEBOOK_APPSECRET

I understand that it can be done while calling the sails lift but is it by any means possible to set these values inside the app so that I only have to call

sails lift

without any of those variables and those should be set automatically ? Or what is the best way to achieve what I am trying to do here ?

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104

3 Answers3

9

You should set the environment-variables outside of your App.

Instead of sails lift you could also use node app.js. With that you can define environment-variables for your node-application with:

$> FOO='bar' node app.js

In your case:

$> FACEBOOK_APPID='232322a22' FACEBOOK_APPSECRET='mysecrete' node app.js

If you want to set this vars in your app (I wouldn't suggest that!) you could set them before including your npm-module. For example in your config/bootstrap.js:

module.exports.bootstrap = function(cb) {

  process.env.FACEBOOK_APPID = "myvar";
  process.env.FACEBOOK_APPSECRET = "mysecrete";
  sails.facebook = require("yourmodule");

  cb();
};
mdunisch
  • 3,627
  • 5
  • 25
  • 41
  • That's what I am currently doing but this seems error prone to me just in case someone forgets to pass these parameters while launching the app. If it is something I am going to always use when I lift the server, what would be the best way to achieve it ? – Mandeep Singh Aug 20 '14 at 13:27
  • 1
    As the name said, this vars should set outside of your app- but I edited my solution for you to set them into your sails-app. – mdunisch Aug 21 '14 at 08:52
4

Here is a good link for setting environment variables when "Production", "Staging" and "Development" in Sails feels fragmented.

Here is a quick video tutorial which simply explains how to create environment variables in node.js

Step1: Install the package dotenv as dependency

$ npm install dotenv --save

Step2: Then require dotenv in app.js and load the .env file

var dotenv = require('dotenv');
dotenv.load();

Step3: Create a .env file in the root folder (ie. at the same document level as app.js file)

Step4: Add your environment variables

S3_KEY=enfiownqefniqofewqofnieqwvewlk
S3_SECRET=123456789

Now you can access those environment variables anywhere in the app (probably in some config/ files):

return {
  key: process.env.S3_KEY,
  secret: process.env.S3_SECRET,
  region: 's3-eu-west-1',
  bucket: 'myBucket',
  s3params: {
    ACL: 'public-read'
  }
}

Credit goes to Noah Bass for providing this tutorial.

Eli Peters
  • 406
  • 4
  • 10
  • 1
    There is one problem with this approach: when using `sails generate`, or any other `sails command`, the 'app.js' file is not required, thus the env variables are not included. – DUzun Jun 16 '19 at 21:08
  • There is another problem.`TypeError: dotenv.load is not a function`. To fix that - just add at the beginning of app.js `require('dotenv').config();` instead of what @Eli Peters recommended – 1nstinct Mar 07 '21 at 05:23
0

SailsJS has a way to set them in the environment that overrides any configs in the config files (production.js for example).

The link to the doc for this use is here: https://sailsjs.com/documentation/concepts/configuration#?setting-sailsconfig-values-directly-using-environment-variables

You can set the env variables like so if using the sails console (this is from the docs)

sails_security__cors__allowOrigins='["http://somedomain.com","https://anotherdomain.com:1337"]' sails console

If you are using a cloud environment, you can set it within the environment.

For setting within the app itself, use the config files structure config/env/* where * is staging.js production.js

Make your request like so

module.exports.getAccessToken = async () => {
  const tokenData = await axios({
    method: "POST",
    url: `${sails.config.custom.externalAPI.auth}`,
    headers: { "Content-Type": "application/json" },
    data: {
      grant_type: "client_credentials",
      client_id: `...`,
      client_secret: `...`,
      audience: ...``,
    },
  })

Do not use dotenv of process.env to set as sails provides you a way to do this within the framework.

OctaviaLo
  • 1,268
  • 2
  • 21
  • 46