使用Meteor.js检测环境?

Abi*_*ilW 8 environment-variables meteor

有人想出使用Meteor.js来检测托管环境的语法或模式吗?我有Heroku buildpacks工作,并有一个开发/生产环境,但我有点想知道如何让我的应用程序检测它正在运行的环境.

有没有办法让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));
    });
}
Run Code Online (Sandbox Code Playgroud)

还创建了以下内容:

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

在客户端允许以下内容:

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

谢谢Rahul!

Rah*_*hul 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' }
Run Code Online (Sandbox Code Playgroud)