Szukaj…
Zaawansowane konfiguracje środowiska
W przypadku bardziej złożonych aplikacji będziesz chciał zbudować obiekt `` settings.json`` przy użyciu wielu zmiennych środowiskowych.
if(Meteor.isServer){
Meteor.startup(function()){
// this needs to be run on the server
var environment, settings;
environment = process.env.METEOR_ENV || "development";
settings = {
development: {
public: {
package: {
name: "jquery-datatables",
description: "Sort, page, and filter millions of records. Reactively.",
owner: "LumaPictures",
repo: "meteor-jquery-datatables"
}
},
private: {}
},
staging: {
public: {},
private: {}
},
production: {
public: {},
private: {}
}
};
if (!process.env.METEOR_SETTINGS) {
console.log("No METEOR_SETTINGS passed in, using locally defined settings.");
if (environment === "production") {
Meteor.settings = settings.production;
} else if (environment === "staging") {
Meteor.settings = settings.staging;
} else {
Meteor.settings = settings.development;
}
console.log("Using [ " + environment + " ] Meteor.settings");
}
});
}
Określanie parametrów aplikacji za pomocą METEOR_SETTINGS
Zmienna środowiskowa METEOR_SETTINGS może akceptować obiekty JSON i Meteor.settings ten obiekt w obiekcie Meteor.settings . Najpierw dodaj settings.json do katalogu głównego aplikacji z pewnymi informacjami o konfiguracji.
{
"public":{
"ga":{
"account":"UA-XXXXXXX-1"
}
}
}
Następnie musisz uruchomić aplikację przy użyciu pliku ustawień.
# run your app in local development mode with a settings file
meteor --settings settings.json
# or bundle and prepare it as if you're running in production
# and specify a settings file
meteor bundle --directory /path/to/output
cd /path/to/output
MONGO_URL="mongodb://127.0.0.1:27017" PORT=3000 METEOR_SETTINGS=$(cat /path/to/settings.json) node main.js
Do tych ustawień można uzyskać dostęp z Meteor.settings i użyć ich w swojej aplikacji.
Meteor.startup(function(){
if(Meteor.isClient){
console.log('Google Analytics Account', Meteor.settings.public.ga.account);
}
});
Wykrywanie środowiska na serwerze
Zmienne środowiskowe są również dostępne dla serwera za pośrednictwem obiektu process.env .
if (Meteor.isServer) {
Meteor.startup(function () {
// detect environment by getting the root url of the application
console.log(JSON.stringify(process.env.ROOT_URL));
// or by getting the port
console.log(JSON.stringify(process.env.PORT));
// alternatively, we can inspect the entire process environment
console.log(JSON.stringify(process.env));
});
}
Wykrywanie środowiska klienta za pomocą metod meteorologicznych
Aby wykryć środowisko na serwerze, musimy utworzyć metodę pomocniczą na serwerze, ponieważ serwer określi, w jakim środowisku się znajduje, a następnie wywoła metodę pomocniczą od klienta. Zasadniczo po prostu przekazujemy informacje o środowisku z serwera do klienta.
//------------------------------------------------------------------------------------------------------
// 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.");
});
Wykrywanie środowiska klienta za pomocą NODE_ENV
Od wersji Meteor 1.3 Meteor NODE_ENV wyświetla teraz zmienną NODE_ENV na kliencie.
if (Meteor.isClient) {
Meteor.startup(function () {
if(process.env.NODE_ENV === "testing"){
console.log("In testing...");
}
if(process.env.NODE_ENV === "production"){
console.log("In production...");
}
});
}