4
votes
import express, { Request, Response } from "express";
import axios, { AxiosRequestConfig } from "axios";
import fileUpload from "express-fileupload";

const app = express();

app.use(fileUpload());

app.post("somePath", async (req: Request, res: Response) => {
  const { files } = req; // what to do with files here?
  const { originalUrl, hostname } = req;
  const axiosConfig: AxiosRequestConfig = {
    baseURL: "someHostName",
    url: originalUrl,
    method: "post",
    headers: req.headers
    data: req.body // I tried a bunch of stuff here, including putting req.files
  };
  await axios(axiosConfig)
    .then((v) => {
      res.status(200).json(v);
    })
    .catch((e) => {
      res.status(400).json({ errors: [e.message] });
    });
});

This is a dummy file that could be considered the Proxy. I'm receiving a multipart/form-data with FormData() as a body in which the express-fileupload middleware transforms into a pleasant object and puts in a new files property on the request object. I'm attempting to send that same FormData to another backend service using Axios, but I am unsure how to do it. In fact, I've tried several things; from playing with streams to Buffers, to using form-data library. The fact is, I want to keep the request as is (headers, URLs, and all), but make sure I have the FormData still when sending to the other service because that service is basically using express-fileupload as well.

Is there any way to do that?

EDIT: For clarification, I'm simply receiving a 400 error from the backend service saying I did not input any files. But when I log the request headers, it's getting all the same headers as the proxy was getting, including the Content-Length, type, etc.

Did you find a solution for this?Wildan Maulana Syahidillah
Yes, can't put it up now but I should find some time for it eventually.tcaissie
would be useful to post itkevin