Deevanshu Kushwah
Deevanshu Kushwah

Reputation: 1

express http-proxy-middleware upload not working

When i am uploading file from frontend with formdata as body of post request, Backend is returning 400 bad request.

const formData = new FormData();  
formData.append("file", this.fileSelected);  
formData.append("type", "ALLOC");

when i check the backend logs, then its saying file is undefined, It means proxy drops the multipart formdata client --> proxy server --> backend global middlewares

app.use(morgan("dev"));
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
// restream parsed body before proxying
let restream = function(proxyReq, req, res, options) {
    console.log("################");
    console.log(req.body);
    console.log(req.file);
    console.log(req.type);
    console.log(req.filename);
    if (req.body) {
        let bodyData = JSON.stringify(req.body);
        proxyReq.setHeader('Content-Type','application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        proxyReq.write(bodyData);
    }
}

router.use('',checkAuthentication,createProxyMiddleware({
    target: config.MAIN_SERVER_BASE_URL,
    changeOrigin: true,
    on: {
        proxyReq: restream
    }
}));

I need to handle both json request and formdata.

Upvotes: 0

Views: 74

Answers (0)

Related Questions