I am attempting to send an image file from my React frontend to my express API:
fetch('http://localhost:4000/upload/', {
method: 'POST',
body: this.state.picture[0],
})
The file looks correct in the chrome console when I log it:
File {name: "31-614x614.jpg", lastModified: 1553111550472, lastModifiedDate: Wed Mar 20 2019 12:52:30 GMT-0700 (Pacific Daylight Time), webkitRelativePath: "", size: 95101, …}
However when I try to log the file in the request on my backend, I either get undefined or empty request objects depending on how I set it up.
I've tried using multer, busboy, and express-fileupload as recommended in similar stack overflow questions, but have the same issues.
Here's my router:
const Router = require('express').Router();
const imgController = require('./controllers/imgController');
Router.get('/images/', imgController.getImages);
Router.post('/upload/', imgController.postImage);
module.exports = Router;
And the controller where I'm simply trying to see the file being sent:
exports.postImage = (req, res) => {
console.log("IN POST IMAGE")
console.log(req.body)
console.log(req.files)
}
I do get into the controller, but the request never contains the file. Any help is appreciated!