代理 /admin 请求到 Prisma Studio

问题描述 投票:0回答:1

我有一个使用Postgres+Prisma的nestjs应用程序。我的目标是快速创建一个管理面板来管理数据库表。 Prisma studio 在这方面非常适合我。它在 localhost:5555 上运行,因此我在 Nestjs 中创建了中间件,当向 /admin 发出请求时,它会将请求代理到 localhost:5555。 (我使用http-proxy-middleware

//proxy.middleware.ts
@Injectable()
export class ProxyMiddleware implements NestMiddleware {
  private readonly proxy = createProxyMiddleware({
    target: 'http://localhost:5555', // Замените на адрес вашего целевого сервера
    changeOrigin: true,
    pathRewrite: { '^/admin': '' },
  });
  use(req: Request, res: Response, next: () => void) {
    this.proxy(req, res, next);
  }
}
//app.module.ts
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(ProxyMiddleware).forRoutes('admin');
  }
}

因此,它确实将请求重定向到 Prisma Studio,但出现错误:

POST http://localhost:3333/api 404(未找到)

所以,看起来它正在尝试通过获取 localhost:3333/api 来获取 studio api,但它在那里不存在。实际上,我认为它应该获取 localhost:3333/admin/api。 有什么办法可以将此请求重定向到 admin/api 吗?

nestjs http-proxy
1个回答
0
投票

通过添加解决

//app.module.ts
consumer.apply(ProxyMiddleware).forRoutes('/api');

不,我 Prisma Studio 在 /admin 上运行

© www.soinside.com 2019 - 2024. All rights reserved.