How to setup different environments and corresponding configurations under sails.js?

馋奶兔 提交于 2019-12-24 13:16:10

问题


I'm in the process of migrating a 2010 RoR app to Sails.js. From the face of it Sails.js looks like the best framework for migrating the app to Node.js platform. In the process, I came across a limitation - it seems like sails only caters to a development environment through local.js and a production environment.

Under RoR, I had a environments folder under config, which contained dev, test, staging, production, etc. ruby files that contained constants, logging levels, etc. defined for that environment. I am looking for an example of the same structure under Sails.js.

Thanks,

_K


回答1:


The SailsJS folks will tell you that the best practice is to create and deploy a local.js file for each environment, and keep it untracked in your repo.

However, if your project is in a private repo and you are comfortable keeping sensitive information there, you can use process.env.NODE_ENV to write conditionals. For example, here's how you would define different adapters for different environments in the adapters.js file:

var production = {
  'default': 'mongo',
  mongo: {
    module: 'sails-mongo',
    url: "mongodb://[user]:[password]@[host]:[port]/[db-name]",
    schema: true
  }
};

var staging = {
  'default': 'mongo',
  mongo: {
    module: 'sails-mongo',
    url: "mongodb://[user]:[password]@[host]:[port]/[db-name]",
    schema: true
  }
};

var development = {
  'default': 'disk',
  disk: {
    module: 'sails-disk'
  }
};

var setAdapter = function() {
  var env = process.env.NODE_ENV;

  if (env === 'production') {
    return production;
  } else if (env === 'staging') {
    return staging;
  } else {
    return development;
  }
};

var adapters = setAdapter();

module.exports.adapters = adapters;

Just make sure you set the NODE_ENV environment variable on your server, and you're good to go.



来源:https://stackoverflow.com/questions/21102052/how-to-setup-different-environments-and-corresponding-configurations-under-sails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!