sails.js Is it possible to set process.env variables from application? - sails.js

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 ?

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();
};

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.

Related

Verify JWT Token error 'secretOrPrivateKey must have a value'

return failure(new Error('secretOrPrivateKey must have a value'));enter image description here
I think your application is not able to read the environnement variables.
I advice you to use the package dotenv : https://github.com/motdotla/dotenv
After installing it you have to import it at the top of your application like this:
require("dotenv").config();
Specify a file .env at the root of your application and add your variables
JWT_SECRET=12bob12ou2b1ob
NODE_ENV=development
After, you can use the variables like you did in your linked image:
const jwtSecret = process.env.JWT_SECRET

Loopback 4: Specify environment var in the #model

I am trying to get the mongodb collection name from an environment variable defined in .env. It looks like the model is initialised before the .env is read.
#model({
name: 'MyType',
settings: {
mongodb: process.env.COLLECTION_NAME,
},
})
The process.env.COLLECTION_NAME is, at this point, undefined
Any way to externalised the collection name?
Many thanks for the help.
By default, Node.js does not read from .env.
To read from .env, we need to utilize the dotenv package.
Since models are resolved through the Booter during startup, hence we need to call dotenv.config() before app.boot().
Update index.ts as follows:
// At the top of the file
import dotenv from 'dotenv';
...
// Inside the main function
// dotenv.config() must appear before app.boot()
dotenv.config()
...
app.boot();
app.start();
This would cause dotenv to overwrite process.env with the values in .env where necessary.
Further reading:
https://www.npmjs.com/package/dotenv
How do I setup the dotenv file in Node.js?
in your
src/index.ts
import {ApplicationConfig, MyApplication} from './application';
require('dotenv').config();
export * from './application';
and your .env file in the "root" of your project

Include node module as a file to inject

I want to include /node_modules/es6-shim/es6-shim.min.js into the client side in Sails v0.11.
I've tried including it into the pipeline as such:
var jsFilesToInject = [
// Load sails.io before everything else
'js/dependencies/sails.io.js',
/* INCLUDE NODE MODULE */
'/node_modules/es6-shim/es6-shim.min.js',
// Dependencies like jQuery, or Angular are brought in here
'js/dependencies/**/*.js',
// All of the rest of your client-side js files
// will be injected here in no particular order.
'js/**/*.js',
// Use the "exclude" operator to ignore files
// '!js/ignore/these/files/*.js'
];
Is this possible? I don't really want to use bower or a CDN, I would like to install/update the dependency via npm.
The simplest way to accomplish this would be to leave the pipeline.js file alone and just make a symlink inside of assets/js pointing to the file you want, e.g.:
ln -s ../../node_modules/es6-shim/es6-shim.min.js assets/js/es6-shim.min.js
Next time you run sails lift, Grunt will see the new Javascript file in your assets/js folder and process it with the rest.
If this is for some reason not an option, you'll need to add a new subtask to the tasks/copy.js Grunt task:
dev_es6: {
files: [{
expand: true,
src: ['./node_modules/es6-shim/es6-shim.min.js'],
dest: '.tmp/public/js'
}]
}
and then add that to the compileAssets task in tasks/register/compileAssets:
module.exports = function (grunt) {
grunt.registerTask('compileAssets', [
'clean:dev',
'jst:dev',
'less:dev',
'copy:dev',
'copy:dev_es6', // <-- adding our new subtask
'coffee:dev'
]);
};

Sharing a public tree in an ember-cli addon

I am completely puzzled when I read all the information I can gather about sharing a public assets directory from an ember-cli addon.
Is there anybody having it working around here? Any ref to an example addon doing it would also be appreciated...
So... I finally found a way to share the static assets:
- I placed the files in vendor/assets directory
- Declared the files to shared (each file...) into the addon's index.js file # addon's root
app.import('vendor/assets/my_image.png');
An interesting option of app.import statement I found in my searches is destDir, which allows to customize the target publication path of the asset:
app.import('vendor/assets/a/b/c/my_image.png', { destDir: 'x/y' });
will publish my_image.png # URL /assets/x/y/my_image.png
Hoping this will help others to save time...
Assets of addons are available under a namespace. For example if there is a file in public/assets/image.png in your addon, this file is available under /my-addon/assets/image.png.
If you don't want to use a namespace, you can overwrite the treeForPublic hook in the addon definition as demonstrated in this gist:
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
module.exports = {
name: require('./package').name,
treeForPublic: function(tree) {
const assetsTree = new Funnel('public');
return mergeTrees([tree, assetsTree], {
overwrite: true,
});
},
};

Simple auth addon seems to not be reading env config

I am following the example here, and I have this in my config/environment.js file:
ENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:devise',
routeAfterAuthentication: 'landing-pages'
};
However, after my app authenticates it tries to go to the index route. I confirmed the configuration variable used had index as the routeAfterAuthentication property by adding a breakpoint in the sessionAuthenticationSucceeded method of the library.
I tried importing the configuration in the environment.js file ES6-style, but that doesn't seem possible.
Ember Simple Auth actually still relies on the window.ENV configuration variable, so you'll need to add it to your configuration. Do it like this:
window.MyAppENV = {{ENV}};
+ window.ENV = window.MyAppENV;
window.EmberENV = window.MyAppENV.EmberENV;
When used with the Ember CLI Simple Auth addon, Ember Simple Auth uses the ENV['simple-auth'] configuration set in config/environment.js like below:
...
var ENV = {
...
};
ENV['simple-auth'] = {
routeAfterAuthentication: 'some.route.name.you.choose'
};
...