express-http-proxy icon indicating copy to clipboard operation
express-http-proxy copied to clipboard

proxyReqBodyDecorator does not pass modified body

Open holopekochan opened this issue 7 years ago • 2 comments

proxy.js

const express = require('express')
const proxy = require('express-http-proxy');
const bodyParser = require('body-parser');
const multer = require('multer');

const app = express();
var upload = multer();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/api',upload.single('file'), proxy('localhost:4000', {
  proxyReqBodyDecorator: function(bodyContent, srcReq) {
    bodyContent.file = srcReq.file;
    console.log(JSON.stringify(bodyContent));
    return bodyContent;
  },
  proxyReqPathResolver: function(req) {;
    return require('url').parse(req.url).path;
  }
}));

api.js

const express = require('express')
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

router.post('/uploadfile', async function(req, res){
	console.log(req);
});

This is my code to proxy to localhost:4000

From console.log(JSON.stringify(bodyContent)) of proxy.js , I can see the correct bodyContent which I had modified. However, in api.js, I still receive empty body inside req. proxyReqBodyDecorator does not pass the modified proxyReqBodyDecorator

Result of console.log(JSON.stringify(bodyContent)) from proxy.js

{"file":{"fieldname":"image","originalname":"152545959171533975.png","encoding":"7bit","mimetype":"image/png","buffer":{"type":"Buffer","data":[137,80,78,71,13,10,26,10,0,0,0,13,73...}

Result of console.log(req.body) from api.js

{}

Method calling API: with POST and form-data body

holopekochan avatar Aug 02 '18 18:08 holopekochan

same for me

eljefedelrodeodeljefe avatar Aug 21 '18 10:08 eljefedelrodeodeljefe

it's a long shot but did you @eljefedelrodeodeljefe or @holopekochan find a solution for this issue without rebuilding it with a different package like http-proxy-middleware?

kepi0809 avatar Jun 13 '23 14:06 kepi0809