首页 > 解决方案 > 在控制器 NestJS 中的对象内获取 .env 变量

问题描述

我创建了 .env

APP_PORT=3001
DEST=C:\data\formations

我在控制器承包商中有 configService:ConfigService。我想DEST在这段代码中从 .env 获取目的地。

@UseInterceptors(FileInterceptor('file', {
  storage: diskStorage({
    destination: /*here*/ ,
    filename: (req, file, cb) => {
      const filename: string = path.parse(file.originalname).name.replace(/\s/g, '')
      const extension: string = path.parse(file.originalname).ext
      cb(null, `${filename}${extension}`)
    }
  })
}))

当我在这个其他对象中使用它时,我得到一个错误。

标签: javascriptnestjs

解决方案


您应该使用ConfigModule获取环境变量。

查看文档:https ://docs.nestjs.com/techniques/configuration


推荐阅读