用Meteor.js检测环境? [英] Detecting environment with Meteor.js?
问题描述
有没有办法让node.js检测它运行的端口?我希望可能有一些像app.address()。端口的低级别的东西,但似乎不起作用。
编辑:这是解决方案为我工作。请注意,以下需要在服务器上运行,因此需要包含在server\server.js或类似的文件中。
if(Meteor.is_server){
Meteor.startup(function(){
//我们想要检查root_url,所以我们知道我们在
console.log(JSON.stringify(process.env.ROOT_URL));
//如果我们要检查其他进程环境变量
//console.log(JSON.stringify(process.env));
});
}
还创建了以下内容:
Meteor.methods({
getEnvironment:function(){
if(process.env.ROOT_URL ==http :// localhost:3000){
returndevelopment;
} else {
returnstaging;
}
}
}) ;
其中允许客户端的以下内容:
Meteor.call(getEnvironment,function(result){
console.log(你的应用程序正在运行+结果+environment);
});
谢谢拉胡尔!
您可以检查服务器上的 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' $ b 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'}
Anybody figure out syntax or a pattern yet for detecting hosting environment using Meteor.js? I've got Heroku buildpacks working, and have a dev/production environment, but I'm kind of drawing a blank on how to have my app detect which environment it's running in.
Is there a way to have node.js detect which port it's running on? I was hoping there might be something low-level like app.address().port, but that doesn't seem to work...
Edit: This is the solution that worked for me. Note that the following needs to be run on the server, so it needs to be included in server\server.js, or a similar file.
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));
});
}
Also created the following:
Meteor.methods({
getEnvironment: function(){
if(process.env.ROOT_URL == "http://localhost:3000"){
return "development";
}else{
return "staging";
}
}
});
Which allows for the following on client side:
Meteor.call("getEnvironment", function (result) {
console.log("Your application is running in the " + result + "environment.");
});
Thanks Rahul!
You can inspect the process.env
variable on the server to find information about the current environment, including the port:
{ 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' }
这篇关于用Meteor.js检测环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!