Nestjs swagger ui 在部署到 vercel 时不加载样式,但在本地运行良好
我使用以下配置添加了 vercel.json 并部署到 vercel。
{
"version": 2,
"builds": [
{
"src": "src/main.ts",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "src/main.ts",
"methods": ["GET", "POST", "PUT", "PATCH", "DELETE"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
主要.ts
const swaggerConfig = new DocumentBuilder()
.setTitle('Tansfun')
.setDescription('API for Tansfun')
.setVersion('1.0')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'APIKey',
name: 'APIKey',
description: 'Enter API Key',
in: 'header',
},
'APIKey-auth',
)
.build();
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const …Run Code Online (Sandbox Code Playgroud) 目前我已经设置了一个带有turborepo的monorepo,其中Nestjs作为BE,Nextjs作为FE。
我想重用 prisma 定义,所以很自然地将它分成自己的包并实现自己的 tsconfig。在我的数据库包的索引(棱镜所在的位置),我有这个简单的代码:
export * from "@prisma/client";
我的后端和前端现在都具有相同的依赖项:
backend -> database和frontend -> database
我的 FE 编译得很好,我可以使用我的 prisma 中的定义,但是 NestJS 应用程序没有在数据库包中编译 TS 并且有这个错误,我认为它与 tsconfig 有关,似乎 NestJS (我的后端)确实如此不想编译私有包依赖项,因此它无法识别“导出”。
core:dev: export * from "@prisma/client";
core:dev: ^^^^^^
core:dev:
core:dev: SyntaxError: Unexpected token 'export'
Run Code Online (Sandbox Code Playgroud)
谁能指出我的回购协议有什么问题吗?
在导入 Nestjs 应用程序之前,我需要先构建数据库包吗?如果是这样,客户端为什么不先构建它就可以工作?
这是我的后端 tsconfig server/core/tsconfig.json:
{
"extends": "tsconfig/server.json",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./",
},
}
Run Code Online (Sandbox Code Playgroud)
这是我的前端 tsconfig(工作正常)apps/web/tsconfig.json:
{
"extends": "tsconfig/nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
和扩展,
tsconfig/server.json:
{ …Run Code Online (Sandbox Code Playgroud) @Injectable()
export class MyService {
...
constructor(@Inject('CONFIG') private config:ConfigOptions){
this.connection= // await initConnection(config); <-- this returns a promise! How to await this?
}
send(payload:string){
this.connection.send(payload)
}
}
Run Code Online (Sandbox Code Playgroud)
如何await异步调用以initConnection()确保服务在使用前已初始化?
可以注意到,当您使用 DTO 而不是管道(取决于具体情况)时,验证主体甚至路由/查询参数可以更简单、更安全且更具可读性。作为一个简单的示例,我喜欢使用 DTO 来验证 id 的想法,就像在 findOne() 路由中一样。所以,有了这样的课程
export class IdDto {
@IsInt()
@IsPositive()
id: number;
}
Run Code Online (Sandbox Code Playgroud)
我可以很好地验证(序列)id。并在路由处理程序中使用它,如下所示
findOne(@Param() { id }: IdDto)
Run Code Online (Sandbox Code Playgroud)
问题是当我有多个 id 作为路由参数时。显然我必须以不同的方式命名它们,然后我就不能再使用这个 DTO 了。除非我为像这样的每种情况创建一个特定的 DTO,只需重命名字段即可。在这种情况下,是否有任何方法可以维持此 DTO(或任何其他)的使用?或者真的没有其他选择吗?
当然,我可以建造自己的管道。只是 DTO 方法要简单得多,而且类验证器装饰器会自动显示验证失败的具体步骤。但也许这是除了多个类似的 DTO 之外的唯一选择。
预先致谢,并致以最诚挚的问候。
PS:在我提到的多个 id 的情况下,情况会稍微复杂一些。但如果我仍然只有一个 id,但想在路由中以不同的方式命名它?同样的问题也适用,以防它可能更简单。
等待docker容器创建并运行nest,修改main.ts文件,nest监听不执行热重加载
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.2.4",
"@types/node": "18.11.18",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.3.1",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.1",
"typescript": "^4.7.4"
}
Run Code Online (Sandbox Code Playgroud)
###################
# BUILD FOR LOCAL DEVELOPMENT
###################
FROM node:18-alpine As development
WORKDIR …Run Code Online (Sandbox Code Playgroud) 我有一个使用 NestJS 的微服务设置。但是,我在运行时收到以下错误。
this.assertNotInPreviewMode('listen');
^
TypeError: this.assertNotInPreviewMode is not a function
Run Code Online (Sandbox Code Playgroud)
我已经使用安装了 NestJS 微服务包npm i --save @nestjs/microservices。不知道为什么该功能不存在。
我创建了一个auth middlewere来检查每个请求,中间是使用服务器(只有在req.connection中找不到数据).我正在尝试将服务注入到我的中间,我一直得到相同的错误"Nest无法解析AuthenticationMiddleware(?)的依赖关系.请验证当前上下文中是否有[0]参数可用."
AuthenticationModule:
@Module({
imports: [ServerModule],
controllers: [AuthenticationMiddleware],
})
export class AuthenticationModule {
}
Run Code Online (Sandbox Code Playgroud)
AuthenticationMiddleware:
@Middleware()
export class AuthenticationMiddleware implements NestMiddleware {
constructor(private service : UserService) {}
resolve(): (req, res, next) => void {
return (req, res, next) => {
if (req.connection.user)
next();
this.service.getUsersPermissions()
}
}
Run Code Online (Sandbox Code Playgroud)
ServerModule:
@Module({
components: [ServerService],
controllers: [ServerController],
exports: [ServerService]
})
export class ServerModule {}
Run Code Online (Sandbox Code Playgroud)
ApplicationModule:
@Module({
imports: [
CompanyModule,
ServerModule,
AuthenticationModule
]
})
export class ApplicationModule implements NestModule{
configure(consumer: MiddlewaresConsumer): void {
consumer.apply(AuthenticationMiddleware).forRoutes(
{ path: …Run Code Online (Sandbox Code Playgroud) 我有一个运行在端口3000上的Node.js应用程序(使用新的NestJS)框架。作为数据库,我通过TypeORM使用MySQL。在本地,一切正常。我在将其码头化时遇到问题。
我的TypeORM配置:
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "nest",
"entities": ["src/**/*.entity{.ts,.js}"],
"synchronize": true
}
Run Code Online (Sandbox Code Playgroud)
我docker-compose.yml的如下:
version: "3"
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: nest
MYSQL_USER: root
MYSQL_PASSWORD: root
networks:
- new
nest:
image: grimscythe/nest-sample
depends_on:
- db
ports:
- 3000:3000
networks:
- new
networks:
new:
Run Code Online (Sandbox Code Playgroud)
我一直在阅读有关此特定方案的文档,所有文档都应该可以正常工作。扑向MySQL容器表明数据库正在正常运行。但是,Node框架随处可见Unable to connect to the database...。我在docker-compose.yml文件中丢失了什么吗?
NestJS无法解析UsersModule的依赖项。错误:
Error: Nest can't resolve dependencies of the UsersModule (?). Please verify whether [0] argument is available in the current context.
app.module.ts:
@Module({
imports: [
ConfigModule,
DatabaseModule,
GraphQLModule,
UsersModule,
],
providers: [
ErrorService,
],
exports: [
DatabaseModule,
ErrorService,
],
})
export class AppModule implements NestModule {}
Run Code Online (Sandbox Code Playgroud)
users.module.ts:
@Module({
imports: [
DatabaseModule,
ErrorService,
],
providers: [
UsersService,
...usersProviders,
UserResolver,
],
})
export class UsersModule {
constructor(private readonly errorService: ErrorService) {}
}
Run Code Online (Sandbox Code Playgroud)
问题是此ErrorService,但是例如数据库模块以类似的方式使用,并且可以正常工作而没有任何错误。我有点困惑)也许有人会帮忙。谢谢。
I set up a ConfigService as described in docs https://docs.nestjs.com/techniques/configuration
how can I use this service with the the TypeOrmModule?
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
Run Code Online (Sandbox Code Playgroud) nestjs ×10
node.js ×5
typescript ×3
docker ×2
async-await ×1
mysql ×1
nest ×1
next.js ×1
reactjs ×1
swagger ×1
swagger-ui ×1
turborepo ×1