2017-01-14 46 views
0

我在我所製成的beforeCreate方法這樣的模型Transactions運行的函數:僅在帆JS生產環境(節點JS框架)

beforeCreate(values,cb){ 
    //I want this code to be run in just production enviroment, not in devlopement env 
    EmailService.sendMail(values.email,values.data); 
    cb(); 
} 

在這裏,我已作出服務EmailService這將發送郵件給用戶。但我確實希望這隻能在production enviroment之內活動,而不是在發展環境中。

我不想評論這一行,因爲我有很多其他事件只能在生產環境中觸發,而不是在測試環境中觸發。這個怎麼做?

回答

1

在應用程序代碼,您可以access the environment through the global config,像這樣:sails.config.environment

beforeCreate(values,cb){ 
    // Production 
    if (sails.config.environment === "production") { 
     EmailService.sendMail(values.email,values.data); 
    } 
    cb(); 
} 

您的Sails.js應用程序默認在開發環境中運行。您可以在生產模式下運行setting the NODE_ENV environment variableNODE_ENV=production node app.js)或運行sails lift --prod

如果你願意,你還可以選擇設置環境config/local.js代替:

module.exports = { 
    /*************************************************************************** 
    * The runtime "environment" of your Sails app is either typically   * 
    * 'development' or 'production'.           * 
    *                   * 
    * In development, your Sails app will go out of its way to help you  * 
    * (for instance you will receive more descriptive error and    * 
    * debugging output)              * 
    *                   * 
    * In production, Sails configures itself (and its dependencies) to  * 
    * optimize performance. You should always put your app in production mode * 
    * before you deploy it to a server. This helps ensure that your Sails * 
    * app remains stable, performant, and scalable.       * 
    *                   * 
    * By default, Sails sets its environment using the `NODE_ENV` environment * 
    * variable. If NODE_ENV is not set, Sails will run in the    * 
    * 'development' environment.            * 
    ***************************************************************************/ 

    // environment: process.env.NODE_ENV || 'development' 
    environment: 'production' 
} 
2
beforeCreate(values,cb){ 
    //I want this code to be run in just production enviroment, not in devlopement env 
    if (sails.config.environment === 'production') { 
    EmailService.sendMail(values.email,values.data); 
    } 
    cb(); 
} 
+0

將它的工作時,我沒有使用帆舉,我使用節點app.js? –

+1

您需要將NODE_ENV環境變量設置爲生產服務器上的「生產」。然後使用節點app.js來運行您的sails應用程序。無論如何,帆船升降機並非用於生產用途。 來測試它本地你可以運行你的應用程序:NODE_ENV =生產節點app.js –