Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js -- Accessing local.js environment settings in controllers

In production I have AWS credentials stored as heroku config variables.

In development I want to include the config details in config/local.js, but how do I access the config details in a controller?

local.js contains:

module.exports = { aws_key: "...", aws_secret: "..." }

In my controller I have tried aws_key, config.aws_key, and others - but no luck. Is there a main app namespace that I can use to scope into the properties exported by local.js?

I am new to sails and I feel like this should be straight forward - any help would be appreciated.

like image 494
Drew Avatar asked Jan 22 '14 18:01

Drew


People also ask

How does sails JS work?

Sails. js uses Grunt as a build tool for building front-end assets. If you're building an app for the browser, you're in luck. Sails ships with Grunt — which means your entire front-end asset workflow is completely customizable, and comes with support for all of the great Grunt modules which are already out there.

Is sails JS framework?

js (or Sails) is a model–view–controller (MVC) web application framework developed atop the Node. js environment, released as free and open-source software under the MIT License. It is designed to make it easy to build custom, enterprise-grade Node. js web applications and APIs.

What sails lift?

lift() Lift a Sails app programmatically. This does exactly what you might be used to seeing by now when you run sails lift . It loads the app, runs its bootstrap, then starts listening for HTTP requests and WebSocket connections.

Who is using sails JS?

Who uses Sails. js? 80 companies reportedly use Sails. js in their tech stacks, including Tutor Platform, Redox Engine, and Vuclip.


1 Answers

Solution found. Step 3 was where I was having trouble.

tl;dr

What I didn't realize was that the module.exports.thing makes the thing object available through sails.config.thing. Good to know.


1) I created a new file at config/aws.js with the contents

// Get heroku config values     
module.exports.aws = {
  key: process.env.AWS_KEY,
  secret: process.env.AWS_SECRET
}

2) In local.js put the actual AWS creds (this won't end up in the repository since sails automatically ignores local.js using gitignore).

aws: {
  key: actual-key,
  secret: actual-secret
}

This allows for local testing where we don't have access to the heroku config settings, while protecting these values from being exposed in a github repo.

3) Now, to access in the controller:

var aws_config = sails.config.aws;

AWS.config.update({
  accessKeyId: aws_config.key,
  secretAccessKey: aws_config.secret
});

like image 143
Drew Avatar answered Oct 02 '22 21:10

Drew