从Nestjs中的.env文件读取端口号
我有.env
文件中定义的详细信息。以下是我的代码。
import { Module } from '@nestjs/common';
import { MailService } from './mail.service';
import { MailController } from './mail.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter} from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { join } from 'path';
@Module({
imports:[
MailerModule.forRoot({
transport:{
host: process.env.SMTP_HOST,
port: 1025,
ignoreTLS: true,
secure: false,
auth:{
user:'[email protected]',
pass:'Ddixit90@@',
},
},
defaults:{
from: '"No Reply" <no-reply@localhost>',
},
template:{
dir:join(__dirname,'templates'),
adapter: new HandlebarsAdapter(),
options:{
strict:false,
},
}
}
)
],
controllers: [MailController],
providers: [MailService],
exports:[MailService]
})
export class MailModule {}
主机:process.env.smtp_host
正常工作。但是,当我编写process.env.smtp_port
时,它说您无法将字符串分配给编号。
当我编写ParseInt(Process.env.smtp_port)
时,它仍然无法正常工作。如何从.env
文件分配port
文件
I have details defined in .env
file. Below is my code.
import { Module } from '@nestjs/common';
import { MailService } from './mail.service';
import { MailController } from './mail.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter} from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { join } from 'path';
@Module({
imports:[
MailerModule.forRoot({
transport:{
host: process.env.SMTP_HOST,
port: 1025,
ignoreTLS: true,
secure: false,
auth:{
user:'[email protected]',
pass:'Ddixit90@@',
},
},
defaults:{
from: '"No Reply" <no-reply@localhost>',
},
template:{
dir:join(__dirname,'templates'),
adapter: new HandlebarsAdapter(),
options:{
strict:false,
},
}
}
)
],
controllers: [MailController],
providers: [MailService],
exports:[MailService]
})
export class MailModule {}
host: process.env.SMTP_HOST
is working properly. but when I am writing process.env.SMTP_PORT
it is saying you can not assign string to number.
when I wrote parseInt(process.env.SMTP_PORT)
it is still not working. how to assign port
from .env
file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
端口: +Process.env.smtp_port
应将其施放到一个数字。如果有不同的东西,也许可以共享您遇到的错误。另一种选择是将配置服务用于MailerModule并使用工厂。这是一个示例实现:
这样做,config.get能够通过属性确定类型。否则,您可以使用
config.get&lt; number&gt;
来定义它。port: +process.env.SMTP_PORT
should work casting it to a number. If there is something different, perhaps share the error you are getting.An alternative would be to use the config service for the MailerModule and use the factory. Here is an example implementation of that:
Doing it this way, config.get is able to determine the type by the property. Else you can define it with
config.get<number>