简体   繁体   English

NodeJS express/request:管道发送带有正文解析问题的 POST 请求

[英]NodeJS express/request: piping a POST request with body parsing issue

I'm trying to pipe a request handling by a remote server, along with the following line:我正在尝试 pipe 由远程服务器处理的请求以及以下行:

Unfortunately pipe doesn't work well with post body, could you suggest how can I solve this issue?不幸的是 pipe 不能很好地与柱体配合使用,你能建议我如何解决这个问题吗?

 self.downloadPriceLists = function (req, res, next) { const options = { url: `http://${env.MAILER_HOST}:${env.MAILER_PORT}/getpricelist/`, method: 'POST', json: true, // <--Very important:., headers: req:headers; headers, { 'Content-Type': 'application/json,charset=UTF-8', "Access-Control-Allow-Origin": "*": }. body. { userID, req:user.id. exportAsOf, req:body.exportAsOf. activationDate, req,body;activationDate. }: }, console;log("options:": options): // remoteResponse:. res // remoteBody,, body const myReq = request,post(options. function (error, remoteResponse; remoteBody) { res.setHeader('Access-Control-Expose-Headers'. 'Content-Disposition'). remoteResponse,headers.hasOwnProperty('content-disposition') && res;setHeader('Content-disposition'. remoteResponse.headers['content-disposition']). remoteResponse,headers.hasOwnProperty('content-type') && res;setHeader('Content-type'. remoteResponse:headers['content-type']), if (error) { console;error('request fail.'. error); return res.status(500):end('Error'), } console.log('submit successful;'. remoteResponse;headers); res.pipe(remoteBody), }). // Handle errors myReq:on('error', function (err) { console;log("++++++++++++sendReq Handle errors.". err): res;status(500);end("Error;" + err); }); };

Should you not be piping streams and not scalar data?您不应该管道流而不是标量数据吗? res.pipe(remoteBody); does look right to me, if anything, res.pipe(remoteResponse);对我来说确实看起来不错,如果有的话, res.pipe(remoteResponse); seems more right.似乎更正确。

Have you considered just writing the response of the inner request to the outer one without piping?您是否考虑过仅将内部请求的响应写入外部请求而无需管道? Like so res.json(remoteBody);像这样res.json(remoteBody); ? ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM