我有一个模型Transactions
,我在其中制作了beforeCreate
方法:
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
中激活,而不是在开发环境中。
我不想评论这一行,因为我还有许多其他事件只能在生产环境中触发,而不是在测试环境中触发。怎么做?
答案 0 :(得分: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();
}
答案 1 :(得分: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 variable(NODE_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'
}