0

I'm trying to create a file upload route on my Cloud Function Express app. I've tried different modules such as formidable, multer and busboy, but none could escape the "Unexpected end of form at Multipart._final" error while running the server with firebase emulator, the error appears on both frontend and Postman I tried to re-write the same exact code to a standalone express app without firebase and it works perfectly, with all of the 3 modules, it is driving me crazy, below you can find the last code I wrote:

const express = require("express");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const mammoth = require("mammoth")
const Busboy = require("busboy")

admin.initializeApp(functions.config().firebase);

const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post("/document/placeholders", async (req, res) => {

try {
    
  const busboy = Busboy({ headers: req.headers });
  busboy.on('file', async (fieldname, file, filename, encoding, mimetype) => {
    const chunks = [];
    file.on('data', (chunk) => chunks.push(chunk));
    file.on('end', async () => {
      const buffer = Buffer.concat(chunks);
      console.log(filename);
      const { value: text } = await mammoth.extractRawText({ buffer });
      console.log('Extracted text:', text);
      res.status(200).send('File received');
    });
  });

  busboy.on('error', (err) => {
    console.error('Error:', err);
    res.status(500).send('Error uploading file');
  });

  req.pipe(busboy)
} catch (error) {
   return res.json({success: false, error: error.message || error})
}
});

Silvano H.
  • 43
  • 1
  • 8

1 Answers1

0

Fix for everybody with my same error: apparently firebase does not need a 3rd party module to parse form-data. Defining the file as req.body works properly

Silvano H.
  • 43
  • 1
  • 8
  • Hi Silvano. I am experiencing the same frustration. Could you elaborate on your solution? – imim Mar 20 '23 at 04:41
  • The only solution I could implement was reading req.body in the express endpoint, that is the file's buffer and can be used in the code, note that for me it worked only if I send just the file without any other content in the form-data, and sending the file from postman makes the api call fail again, only the frontend works – Silvano H. Mar 23 '23 at 10:37
  • Still not working for me. Any ideas? – epus Apr 03 '23 at 18:06