从Nestjs中的.env文件读取端口号

发布于 2025-01-24 16:09:02 字数 1568 浏览 5 评论 0原文

我有.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.
enter image description here

when I wrote parseInt(process.env.SMTP_PORT) it is still not working. how to assign port from .env file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2025-01-31 16:09:02

端口: +Process.env.smtp_port应将其施放到一个数字。如果有不同的东西,也许可以共享您遇到的错误。

另一种选择是将配置服务用于MailerModule并使用工厂。这是一个示例实现:

MailerModule.forRootAsync({
            useFactory: async (config: ConfigService) => ({
                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,
                    },
                },
            }),
            inject: [ConfigService],
        }),

这样做,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:

MailerModule.forRootAsync({
            useFactory: async (config: ConfigService) => ({
                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,
                    },
                },
            }),
            inject: [ConfigService],
        }),

Doing it this way, config.get is able to determine the type by the property. Else you can define it with config.get<number>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文