如何在sails.js下设置不同的环境和相应的配置? [英] How to setup different environments and corresponding configurations under sails.js?

查看:190
本文介绍了如何在sails.js下设置不同的环境和相应的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将2010年的RoR应用迁移到Sails.js。从表面上看,Sails.js看起来像将应用程序迁移到Node.js平台的最佳框架。在这个过程中,我遇到了一个限制 - 似乎帆只能通过local.js和生产环境来适应开发环境。

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.

在RoR下,我在config下有一个环境文件夹,其中包含dev,test,staging,production等包含常量,记录级别等的ruby文件为这个环境。我正在寻找Sails.js下相同结构的例子。

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.

谢谢,

_K


_K

推荐答案

SailsJS的人会告诉你,最好的做法是为每个环境创建和部署一个local.js文件,并保留

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.

但是,如果您的项目是私人回购,并且您很乐意在那里保存敏感信息,则可以使用进程。 env.NODE_ENV 写条件。例如,以下是在 adapters.js 文件中为不同环境定义不同适配器的方法:

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;

只需确保设置 NODE_ENV 环境你的服务器上有变数,你很好去。

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

这篇关于如何在sails.js下设置不同的环境和相应的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆