I use express
to handle post requests and formidable to parse uploaded files:
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import * as formidable from "formidable";
import * as util from "util";
import * as express from "express";
admin.initializeApp();
const app = express();
app.post("/image", (req, res) => {
const form = new formidable.incomingForm();
form.parse(req, async function(err, fields, files) {
res.writeHead(200, { "content-type": "text/plain" });
res.write("received upload:\n\n");
res.end(util.inspect({ fields: fields, files: files }));
});
});
export const api = functions.https.onRequest(app);
I already rewrite source and function in firebase.json. I kept getting error Internal Server Error
when deploying and when serving: TypeError: formidable.incomingForm is not a constructor
after posting my form.
formidable
works well with normal node server and Express server on my machine. I already tried firebase node 8 engine beta, used JS instead of TS and still didn't work. How can I make formidable work with Firebase hosting and cloud function? Please help.
Here are my simple client form:
<form action="http://myfirebaseserver/image" method="post" encType="multipart/form-data">
<input type="file" name="myImage" />
<button type="submit">Upload</button>
</form>
At the moment I'm trying busboy
for alternative but still looking for solutions on formidable
Formidable doesn't work with Cloud Functions, for the same reason that multer doesn't work. Use busboy instead.