如何在不在 NestJS 中解析的情况下获取 URL 查询

How to get URL query withouth parsing it in NestJS

大家好,我正在尝试按原样获取包含所有查询参数的路由,而不对其进行解析。 我的路线是这样的:

http://somewebsite/orders?key1=value1&key=value

我不想用

@Query()

那个 returns 具有 key/value 对的对象 我只想获取后面所有内容的纯字符串值? 所以我想得到这样的东西

string = "key1=value1&key=value"

编辑

controller.ts

@Get('/orders')
getOrders(
    @Query(ValidateQueryPipe) query: QueryParameters): Subscription {
    // here I want to have my query as a string not as an object. 
    })
}

所以当我从邮递员那里向我的路线发送请求时,我将能够拥有我传递的所有 key/value 对,但作为一个字符串...

谢谢

尝试传递request object to your function and get the originalUrl,查询字符串应该位于

import { Req } from '@nestjs/common';
import { Request } from 'express';

getOrders(@Req() request: Request): Subscription {
  const regex = /(?<=\?).*$/gm;
  const result = request.originalUrl.match(regex);

  if (result) {
    const query = result[0];
  }
}