简体   繁体   中英

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:

Unfortunately pipe doesn't work well with post body, could you suggest how can I solve this issue?

 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); 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); ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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