expressjs / multer

Node.js middleware for handling `multipart/form-data`.
MIT License
11.57k stars 1.05k forks source link

How to make use of multer when using an API proxy? #430

Closed ghost closed 4 years ago

ghost commented 7 years ago

My application is made using react-redux-starter-kit, and therefore, I have endpoints in another server that are accessed from the front-end by Redux's actions.

I need to upload images, but the server in this react-redux-starter-kit is now express, and I can't find a way for it to work. Below's my API Proxy code:

app.use('/api', (req, res, next) => {
  delete req.header.host

  const options = {
    url: req.url,
    baseUrl: config.api_host,
    headers: JSON.parse(JSON.stringify(req.headers)),
    method: req.method
  }

  if (req.is('application/json')) {
    options.json = true
    options.body = req.body
  }

  request(options).pipe(res)
})

I'm passing the uploaded image to the API using FormData, like this:

onSubmit = (e) => {
  e.preventDefault();

  const { uploadedImage } = this.state;
  const data = new FormData();

  data.append('image', uploadedImage[0]);

  if (uploadedImage.length > 0) {
    this.props.uploadImage(this.props.transaction.id, data);
  }
}

Action:

export function uploadImage({transactionId, data}) {
  return (dispatch, getState) => {
    return dispatch({
      [CALL_API]: {
        endpoint: `/api/transactions/${transactionId}/bank-deposit`,
        method: 'POST',
        body: {data},
        types: [UPLOAD_IMAGE, UPLOAD_IMAGE_SUCCESS, UPLOAD_IMAGE_FAIL]
      }
    })
  }
}

Like this, the data received in the API is null. Please, anyone, help me. Thank you.

gireeshpunathil commented 5 years ago

close candidate, user unavailable for querying for additional info