Firebase hosting and cloud functions don't work with formidable: incomingForm is not a constructor

戏子无情 提交于 2020-02-26 03:41:05

问题


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


回答1:


Formidable doesn't work with Cloud Functions, for the same reason that multer doesn't work. Use busboy instead.



来源:https://stackoverflow.com/questions/52707164/firebase-hosting-and-cloud-functions-dont-work-with-formidable-incomingform-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!