I am using the express server as an API gateway to my microservices, in which I am processing images and store them in a database.
the problem summarizes as follows:
in the express server, I need to forward the image received in a post request from the client and forward it to the web service that can handle image processing.
It turns out that I need to parse the image before I can forward it to the next API, the error I am getting is
Unexpected token - in JSON at position 0
Her is my client that sends the image to the express server(my gateway)
function uploadWithFormData() {
const formData = new FormData();
formData.append("file", file);
fetch('/upload', {
method: "POST",
headers: {
"content-type": "application/json",
},
body: data
}).then(handleResponse)
.catch(handleError);
uploadImage(formData);
}
and this is my express serve that should handle the request and forward it to another web service
app.post("/upload", function (req, res) {
const form = formidable({ multiples: true });
form.parse(req, (err, fields, files) => {
const response = res.json({ fields, files });
let formData = new FormData();
formData.append('file', fs.createReadStream(req.files.file.path), req.files.file.name);
uploadImageAPI(response).then(
res.status(201).send()
).cache(res.status(412).send())
});
I have tried to make some consol.log inside the function to see the req but the request fails before it enters the function, because express could not pars the image. I saw some people doing this using the multer library but I could not mange that in my case any suggestions?