我正在使用 nest-modules/mailer
发送电子邮件,但在 MailerModule
的配置设置过程中遇到一些问题。
(1) 之前,我在 main.ts
中的配置中使用原始 dotenv
包,如下所示:
dotenv.config({
path: 'src/config/config.env',
});
但我无法将配置分配给 app.module.ts
中的 MailerModule
。
(2) 然后我尝试使用 @nesjs/config
设置配置,app.module.ts
如下所示:
import config from 'src/config/config';
@Module({
controllers: [
//...
],
providers: [
//...
],
imports: [
HttpModule,
ConfigModule.forRoot({
load: [config]
}),
MailerModule.forRoot({
transport: {
ignoreTLS: true,
secure: false, // true for 465, false for other ports
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.EMAILDEV_INCOMING_USER,
pass: process.env.EMAILDEV_INCOMING_PWD
},
},
defaults: {
from: `'nest-modules' ${process.env.EMAILDEV_INCOMING_}`, // outgoing email ID
},
template: {
},
})
]
})
export class AppModule implements NestModule {
//...
}
所以我无法使用 configService
和 process.env.*
加载 MailerModule
的配置。
我应该如何解决这个问题?
最佳答案
我在现阶段找到了如何正确解决这个问题的方法:
异步加载EmailModule
(以及其他需要使用配置的内容)。
imports: [
HttpModule,
MailerModule.forRootAsync({
useFactory: async () => {
return {
transport: {
ignoreTLS: true,
secure: false, // true for 465, false for other ports
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.EMAILDEV_INCOMING_USER, // generated ethereal user
pass: process.env.EMAILDEV_INCOMING_PWD // generated ethereal password
},
},
defaults: {
from: `'nest-modules' ${process.env.EMAILDEV_INCOMING_}`, // outgoing email ID
},
template: {
dir: process.cwd() + '/src/shared/static/views',
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
}
}
})
]
关于typescript - 如何正确设置 NestJS 中的配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62846785/