Does anyone know the best way to handle prod/dev/test config switching in sails? I really like how actionhero.js automatically loads config/environment/{env}.js based on the contents of NODE_ENV but I don't see a built-in way to do something similar in sails. I noticed that sails will load any file in config/ during server bootstrap so my hacky solution for now is to setup the following:
config/
|-- local.js
|-- environment/
|---- production.js
|---- staging.js
|---- development.js
|---- testing.js
Then in each {env}.js file, I just extend config like this:
if (process.env.NODE_ENV === 'production') {
// Enter any environment specific config changes
config.db = {
db_host: foo,
db_port: bar
}
config.otherThing = {
somevar: 'someval'
}
Since Sails 0.10-rc6 this got improved and you can now add an env
subfolder in config
to change settings for different environments.
So you could simply add a file /config/env/development.js
or /config/env/production.js
that can override all the necessary settings
See https://github.com/balderdashy/sails/pull/1638 for more details.
Example to change the port and database adapter, e.g. for production
environment in your production.js
:
module.exports = {
port: 80
};
module.exports.models = {
// Your app's default connection.
// i.e. the name of one of your app's connections (see `config/connections.js`)
//
// (defaults to localDiskDb)
connection: 'someMongodbServer'
};
Simply go to config/env/production.js & set port and environment manually
And use NODE_ENV
For Windows use
set NODE_ENV=production
For MAC/Linux use export NODE_ENV=production
OR
sails lift --prod
I did this by making config/local.js
require additional config files depending on process.env.NODE_ENV
.
In config/local.js
var fs = require('fs'),
lodash = require('lodash');
// config.local.js
module.exports = (function () {
var defaults = {
env: process.env.NODE_ENV || 'development',
port: process.env.PORT || 1337,
config: {
paths: {
environments: __dirname + '/environments'
}
}
};
var envConfigPath = defaults.config.paths.environments + '/' + defaults.env + '.js';
var environment = {};
if (fs.existsSync(envConfigPath)) {
var environment = require(envConfigPath);
logger.info('Loaded environment config for ' + defaults.env + '.');
} else {
logger.warn('Environment config for ' + defaults.env +' not found.');
}
return _.merge(defaults, environment);
}());
In .gitignore
:
# config.local.js
config/environments
This gives you:
env
by defining them in config/environments/{env}.js
sails.config{env}
holding all your environment specific configuration, just as your solution does{env}
files{env}
filesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With