使用Meteor.js检测环境?

时间:2013-01-06 17:17:23

标签: environment-variables meteor

有人想出使用Meteor.js来检测托管环境的语法或模式吗?我已经让Heroku buildpack工作了,并且有一个开发/生产环境,但是我在如何让我的应用程序检测它运行的环境方面有点空白。

有没有办法让node.js检测到它在哪个端口上运行?我希望有一些像app.address()。port这样的低级别,但这似乎不起作用......

编辑:这是适合我的解决方案。请注意,需要在服务器上运行以下内容,因此需要将其包含在server \ server.js或类似文件中。

if (Meteor.is_server) {
    Meteor.startup(function () {
        // we want to be able to inspect the root_url, so we know which environment we're in
        console.log(JSON.stringify(process.env.ROOT_URL));

        // in case we want to inspect other process environment variables
        //console.log(JSON.stringify(process.env));
    });
}

还创建了以下内容:

Meteor.methods({
  getEnvironment: function(){
    if(process.env.ROOT_URL == "http://localhost:3000"){
        return "development";
    }else{
        return "staging";
    }
  }
 });    

在客户端允许以下内容:

 Meteor.call("getEnvironment", function (result) {
      console.log("Your application is running in the " + result + "environment.");
 });

谢谢Rahul!

3 个答案:

答案 0 :(得分:13)

您可以检查服务器上的process.env变量,以查找有关当前环境的信息,包括端口:

{ TERM_PROGRAM: 'Apple_Terminal',
  TERM: 'xterm-256color',
  SHELL: '/bin/bash',
  TMPDIR: '/var/folders/y_/212wz0cx5vs20yd7y2psnh7m0000gp/T/',
  Apple_PubSub_Socket_Render: '/tmp/launch-hch25f/Render',
  TERM_PROGRAM_VERSION: '309',
  OLDPWD: '/usr/local/meteor/bin',
  TERM_SESSION_ID: '3FE307A0-B8FC-41AD-B1EB-FCFA0B8B25D1',
  USER: 'Rahul',
  COMMAND_MODE: 'unix2003',
  SSH_AUTH_SOCK: '/tmp/launch-gFCBXS/Listeners',
  __CF_USER_TEXT_ENCODING: '0x1F6:0:0',
  Apple_Ubiquity_Message: '/tmp/launch-QAWKHL/Apple_Ubiquity_Message',
  PATH: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
  PWD: '/Users/Rahul/Documents/Sites/test',
  NODE_PATH: '/usr/local/meteor/lib/node_modules',
  SHLVL: '1',
  HOME: '/Users/Rahul',
  LOGNAME: 'Rahul',
  LC_CTYPE: 'UTF-8',
  SECURITYSESSIONID: '186a4',
  PORT: '3001',
  MONGO_URL: 'mongodb://127.0.0.1:3002/meteor',
  ROOT_URL: 'http://localhost:3000' }

答案 1 :(得分:3)

有一个直接的流星功能:

Meteor.isDevelopment

请参阅:https://docs.meteor.com/api/core.html#Meteor-isDevelopment

和生产:

Meteor.isProduction

都返回一个布尔值

答案 2 :(得分:2)

我使用了NODE_ENV变量的上述变体。有关详细信息,请参阅此处:

http://meteorpedia.com/read/Environment_Variables#Checking%20the%20value%20of%20an%20Environment%20Variable

if Meteor.isServer
  Meteor.methods
    'getEnvironment': -> process.env.NODE_ENV

Meteor.call 'getEnvironment', (err, result) ->
  if result == 'development'
    console.log('In dev env')