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})
}
});