我已經實作了一個NestJs控制器,然后是一個聽從POST請求的facade服務,在請求到達后,它進行一些操作。
現在,它對 "text/plain "內容型別有效,但對 "application/json "內容型別無效。 主體是完全一樣的。
這是一個方法。
這是控制器中的方法:
@Public( )
@Post(SERVER_COVID_A_CASA_CARE_PLAN_NOTIFICATION_PATH)
getNotification(@Req() request: 請求,@Res() response: 回應) {
this.facade.manageCarePlanNotification(request, response)。
}
這就是facade服務中的方法:
這就是facade服務中的方法。
manageCarePlanNotification(request: Request, response: Response) {
let jsonBodyReq = ''/span>;
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)事件。
如果有任何幫助,我們將非常感激! :) 謝謝
uj5u.com熱心網友回復:
怎么又要發明輪子了?
NestJS就在那里。
NestJS的存在是為了從你的背后獲取req/res。它對 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)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/320463.html
標籤:
