1

I am using busboy to upload file in node/express app. I get error Unexpected end of multipart form and the application crash. Whenever I try the route on insomnia, nothing works. Before it was showing that Busboy is not a constructor, so I changed let to const and deleted new, leaving it as it is in the code.

What could be the cause of this error?

this is my code:

router.post('/', async (req, res) => {
    const busboy = Busboy({ headers: req.headers });
    busboy.on('finish', async () => {
        try {
            const { salaoId, servicos } = req.body;
            let error = [];
            let arquivos = [];

            console.log(req.files)

            if (req.files && Object.keys(req.files) > 0) {
                for (let key of Object.keys(req.files)) {
                    const file = req.files[key];

                    const nameParts = file.name.split('.');
                    const fileName = `${new Date().getTime()}.${nameParts[nameParts.length - 1]
                        }`;
                    const path = `servicos/${salaoId}/${fileName}`;

                    const response = await aws.uploadToS3(file, path);

                    if (response.error) {
                        error.push({ error: true, message: response.message })
                    } else {
                        arquivos.push(path)
                    }
                }
            }

            if (error.length > 0) {
                res.json(error[0]);
                return false;
            }

            //CRIAR SERVIÇO
            let jsonServico = JSON.parse(servicos);
            const servicoCadastrado = await Servicos(jsonServico).save();

            //CRIAR ARQUIVO
            arquivos = arquivos.map(arquivo => ({
                referenciaId: servicoCadastrado._id,
                model: 'Servicos',
                caminho: arquivo,
            }));

            await Arquivo.insertMany(arquivos);
            res.json({ servicos: servicoCadastrado, arquivos });

        } catch (error) {
            res.json({ error: true, message: error.message })
        }
    });
    req.pipe(req.busboy)
});

module.exports = router;

this is error:

throw er; // Unhandled 'error' event
      ^

Error: Unexpected end of form
    at Multipart._final (C:\Users\Lenovo\Desktop\projetos\meus-projetos\app-claudiagomes\ws\node_modules\busboy\lib\types\multipart.js:588:17)

2 Answers2

0

I had a similar problem with Next.JS API routes.
Busboy worked well using native Node.JS "http" server, as shown in example on their page, but threw an error in Next.JS. After comparing the requests in "http" server and Next.JS API route handlers I found out they are slightly different.
Difference was just in a very few properties meaning of which I didn't know, but still it gave me the idea that NextJS somehow changes the native http request, so busboy can't work with it.
I read the documentation for Next.JS and found out this

bodyParser is automatically enabled. If you want to consume the body as a Stream or with raw-body, you can set this to false.

So I added this config object, as was suggested in NextJS docs

export const config = {
  api: {
    bodyParser: false,
  },
}

and it fixed the error

Black Beard
  • 1,130
  • 11
  • 18
0

It's simple! I solved it like this

Instead of you use a BusBoy instance

const busboy = Busboy({ headers: req.headers });
busboy.on("finish", async () => {})

Use this:

req. busboy.on("finish", async () => {})

Remember that use the connect-busboy library in the index file