我实现了一个 NestJs 控制器,然后实现了一个监听 POST 请求的 Facade 服务,在请求到达后,它会执行一些操作。
现在,它适用于“text/plain”内容类型,但不适用于“application/json”内容类型。 身体一模一样
这是控制器中的方法:
@Public()
@Post(SERVER_COVID_A_CASA_CARE_PLAN_NOTIFICATION_PATH)
getNotification(@Req() request: Request, @Res() response: Response) {
this.facade.manageCarePlanNotification(request, response);
}
这是门面服务中的方法:
manageCarePlanNotification(request: Request, response: Response) {
let jsonBodyReq = '';
request.on('data', function (data) {
jsonBodyReq += data;
});
request.on('end', () => {
this.manageCarePlanNotificationRequest(jsonBodyReq, response);
});
request.on('error', function (e) {
console.log(e.message);
});
}
json中的请求到达控制器,到达manageCarePlanNotification方法,但没有到达on(data)事件,而通过text/plain请求正确到达(同样发生在on(end)事件中)。
任何帮助将不胜感激! :) 谢谢
又发明轮子做什么?
NestJS 可以从您的背后获取请求/资源。它抽象了 req/res,因此首先它与平台无关(Express/Fastify),而且您不必像您那样关心处理它并陷入麻烦。
当您使用 Nest 时,您应该简单地使用
@Body data: YourDataTypeInJSON
并执行以下操作:
@Public()
@Post(SERVER_COVID_A_CASA_CARE_PLAN_NOTIFICATION_PATH)
getNotification(@Body() data: IDontKnowYourDataType) {
return this.facade.manageCarePlanNotificationRequest(data);
}
是的,就像@marek 指出的那样,你不必重新发明轮子。 Dto 定义将与 @Body() 一起完成这项工作