使用 Meteor 方法檢測客戶端環境
要檢測伺服器上的環境,我們必須在伺服器上建立一個輔助方法,因為伺服器將確定它所在的環境,然後從客戶端呼叫輔助方法。基本上,我們只是將環境資訊從伺服器中繼到客戶端。
placeholderCopy//------------------------------------------------------------------------------------------------------
// server/server.js
// we set up a getEnvironment method
Meteor.methods({
getEnvironment: function(){
if(process.env.ROOT_URL == "http://localhost:3000"){
return "development";
}else{
return "staging";
}
}
});
//------------------------------------------------------------------------------------------------------
// client/main.js
// and then call it from the client
Meteor.call("getEnvironment", function (result) {
console.log("Your application is running in the " + result + "environment.");
});