我想使用环境变量来配置HttpModule
每个模块,从文档中我可以使用这样的配置:
@Module({
imports: [HttpModule.register({
timeout: 5000,
maxRedirects: 5,
})],
})
Run Code Online (Sandbox Code Playgroud)
但我不知道从环境变量(或配置服务)中包含 baseURL 的最佳实践是什么,例如:
@Module({
imports: [HttpModule.register({
baseURL: this.config.get('API_BASE_URL'),
timeout: 5000,
maxRedirects: 5,
})],
Run Code Online (Sandbox Code Playgroud)
这this.config
是undefined
因为它不在课堂上。
从环境变量(或配置服务)设置 baseURL 的最佳实践是什么?
我有 Mongo 用户集合和地址集合。每个地址都由一个用户拥有。这是我的架构类:
export type UserDocument = User & mongoose.Document
@Schema({ timestamps: true })
export class User {
// @Prop({ type: mongoose.Types.ObjectId })
_id: string
@Prop({ required: true })
name: string
@Prop({ required: true, unique: true })
email: string
@Prop({ select: false })
password: string
}
export const UserSchema = SchemaFactory.createForClass(User)
export type AddressDocument = Address & Document
@Schema({ timestamps: true })
export class Address {
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: User.name })
user: User
@Prop()
line1: string
@Prop()
line2?: string …
Run Code Online (Sandbox Code Playgroud) 我是 NestJS 的初学者,我想为以下结构编写一个 DTO -
{
something: {
info: {
title: string,
score: number,
description: string,
time: string,
DateOfCreation: string
},
Store: {
item: {
question: string,
options: {
item: {
answer: string,
description: string,
id: string,
key: string,
option: string
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想为该嵌套数据对象编写一个 DTO。我找不到在 NestJS 中编写嵌套 DTO 的可靠示例。我是 NestJS 的初学者,之前从未使用过 DTO。所以请不要假设我知道一些事情。我将它与 Mongoose 一起使用。
NestJS - 构建后资产和视图文件夹未添加到 dist 文件夹中
我的文件夹结构类似于以下:
/assets
/img
/fonts
/views
failure.hbs
success.hbs
/src
main.ts
/users
users.controller.ts
Run Code Online (Sandbox Code Playgroud) 我正在生成我使用 NESTJS 框架制作的 API 的生产版本,并想知道我应该将哪些文件上传到服务器。当我运行“npm run start: prod”编译时,它会生成“dist”文件夹,但我试图只用它运行,但它不足以运行我的应用程序。我需要将所有文件上传到服务器吗?我做了几个测试,删除了我在开发过程中使用的文件夹,但只有当我在开发模式下都一样时才设法在生产模式下运行。
我在文档中查找了有关此内容的信息,但一无所获。有谁能够帮助我?
谢谢
今天,我试图弄清楚如何在应用程序的后端 (NestJS) 中验证注册表单。我只是想知道是否存在一种验证password
和passwordConfirm
匹配的方法,使用class-validator
包来构建自定义验证器或利用提供的验证器。我在考虑类验证器,而不是字段验证器。
// Maybe validator here
export class SignUpDto {
@IsString()
@MinLength(4)
@MaxLength(20)
username: string;
@IsString()
@MinLength(4)
@MaxLength(20)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {message: 'password too weak'})
password: string;
@IsString()
@MinLength(4)
@MaxLength(20)
passwordConfirm: string;
}
Run Code Online (Sandbox Code Playgroud)
你有什么建议?
我想要一些环境,比如说development
, production
, test
。这些环境应该是独立的,并使用自己的一组配置参数,例如 DB、SERVER_PORT、USER 等。它们不应该位于代码库中,所以我认为它们应该是不同的 .env 文件。也就是说,我应该能够根据活动的环境加载不同的 .env 文件。另外,还不清楚我必须在哪里设置环境切换器。
也许它应该是一个具有 NODE_ENV 参数的单个 .env 文件,该参数可以设置为上述任何值,无论是开发、生产还是测试。根据此参数的值,会自动加载一组必要的配置参数。
我已经阅读了文档,目前对我来说似乎有点令人困惑。
似乎应该有一些配置工厂。
在nestjs中,我明白
imports: [
TypeOrmModule.forFeature([TaskRepository]),
],
Run Code Online (Sandbox Code Playgroud)
创建某种类型的提供者令牌,这样我就可以使用此令牌将存储库注入我的服务(在同一模块中)。
我不明白的是@InjectRepository()
内部是如何工作的,例如:
constructor(
@InjectRepository(TaskRepository)
private taskRepository: TaskRepository,
) {}
Run Code Online (Sandbox Code Playgroud)
InjectRepository
自定义装饰器(应该使用 创建的)如何createParamDecorator
能够注入在模块中注册的提供者?我研究了一下,发现createParamDecorator
没有能力使用DI。如果createParamDecorator
只是创建一个新TaskRepository
实例并返回它,那么为什么我需要TypeOrmModule.forFeature
在模块中导入?有人可以解释一下这在内部是如何工作的吗?谢谢!
我正在使用 nestjs 构建一个 api。添加 typeorm 和 pg 依赖项并添加如下所示的TypeOrmModule.forRoot({})
代码后app.module.ts
。
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoffeesModule } from './coffees/coffees.module';
@Module({
imports: [CoffeesModule, TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'xxx',
database: 'postgres',
autoLoadEntities: true,
synchronize: true
})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
我TypeError: rxjs_1.lastValueFrom is not a function …
我最近从 到typeorm@0.2
版本0.3
,发布的文档中有这样一句话: https: //github.com/typeorm/typeorm/releases/tag/0.3.0这对我来说没有多大意义(在该部分下)的DEPRECATED
):
entities, migrations, subscribers options inside DataSourceOptions accepting string directories support is deprecated. You'll be only able to pass entity references in the future versions.
由此我知道我们现在必须指定实体并且不能使用(或者更好地说,将来无法使用)通配符路径,entities: ['dist/**/*.entity.{ts,js}']
即我们必须使用:entities: [User, AnoherEntity...]
但这migrations
也适用吗?我觉得很困惑,因为迁移是由 typeorm 的 cli 生成的,这意味着我们必须生成迁移1652169197705-SomeMigration
,然后将该文件名及其完整路径添加到数据源的迁移中?migrations: ['1652169197705-SomeMigration'...]
谢谢!
nestjs ×10
typescript ×5
node.js ×3
typeorm ×3
javascript ×2
dto ×1
mongoose ×1
postgresql ×1
production ×1