【发布时间】:2021-01-20 15:48:10
【问题描述】:
所以,这是我的 NEST JS 基本应用程序。
./shared/utils/config/index.ts
export default () => ({
PORT: parseInt(process.env.PORT, 10) || 3000,
TO_PRINT_RESPONSE: JSON.parse(process.env.TO_PRINT_RESPONSE),
});
app.module.ts
import CONFIG from './shared/utils/config/';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [ CONFIG ],
})
]
// some more Module Decorator Config
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthMiddleware)
.forRoutes({ path: '/someurl', method: RequestMethod.ALL });
// some more configuration code.
}
}
main.ts
// AppModule is app.module.ts imported variable
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app: INestApplication = await NestFactory.create(AppModule, {
logger: console,
});
const configService = app.get(ConfigService);
console.log(typeof configService.get<Boolean>('TO_PRINT_RESPONSE'));
/* this is coming as String even when:
* 1. I place <Boolean> as a type (I know its of no use, since it does not change the datatype)
* 2. But in config/index.ts I parsed it in BOOLEAN using JSON.parse()
*/
}
bootstrap();
.env
PORT=5000
TO_PRINT_RESPONSE=true
现在:
- .env 正在通过 dotenv 模块加载(https://docs.nestjs.com/techniques/configuration)
- 在 ./shared/utils/config/index.ts 中保持调试,它会受到影响。
所以,有人可以告诉我,当我以正确的格式(./shared/utils/config/index.ts强>)。
感谢和快乐编码:)
【问题讨论】:
标签: javascript node.js typescript environment-variables nestjs