I am working on a Next Js project that includes an upload image endpoint.
const busboy = require('busboy')
export default function handle(req, res)
{
const bb = busboy({headers: req.headers});
bb.on('file', (fieldname, file, filename, encoding, mimetype) =>
{
res.status(200).send({ message: 'file' }); //not functioning as expected
});
bb.on('close', () =>
{
res.status(201).send({ message: 'close' });
});
bb.on("finish", () =>
{
res.status(202).send({ message: 'finish' });
});
req.pipe(bb);
res.status(203).send({ message: 'failed' });
}
I am encountering an issue with parsing form data using busboy. It seems to be skipping the file, close, and finish events and returning the failed message instead.
This is the event handler for submitting the form which triggers this endpoint:
function onFileUpload(e)
{
e.preventDefault();
let form = new FormData();
form.append("image", file, file.name);
console.log(form)
axios({
method: "post",
url: "http://localhost:3000/api/uploadImage",
data: form,
headers: form.getHeaders
//{'Content-Type': 'multipart/form-data'}
})
.then((res) =>
{
console.log(res);
});
}
Can anyone help identify what might be causing this issue?