多表单数据 req.body 未在 Express 中定义(Firebase 文件上传)

multer form data req.body undefined in Express (Firebase file upload)

我正在尝试构建一个 API 端点来接收来自用户的小音频文件并将其上传到 firebase 存储。我正在使用 multer 来处理表单数据,但是在使用邮递员进行测试时,我不确定 upload.single('file') 是否正常工作,因为 req.body 和 req.file 未定义。我做错了什么吗?

我阅读了很多关于此的 Whosebug 线程,但没有一个有效

代码:

const functions = require('firebase-functions');
const googleStorage  = require('@google-cloud/storage');
const express = require('express');
const cors = require('cors');
const compression = require("compression");
const saltedMd5 = require("salted-md5");
const multer = require("multer");
const path = require("path");

const app = express();

const admin = require('firebase-admin');
admin.initializeApp({
  storageBucket: <bucket address>
});
app.locals.bucket = admin.storage().bucket(<bucketName>);
const db = admin.firestore();

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


const upload = multer({
  storage: multer.memoryStorage(),
});


app.post("/upload", upload.single("file"),  (req, res) => {
  console.log(req.file); //undefined
  console.log(req.files); //undefined
  console.log(req.body); //[Object: null prototype] {}
  console.log(req.name); //undefined
  const name = saltedMd5(req.file.originalname, "SUPER-S@LT!");
  const fileName = name + path.extname(req.file.originalname); //error 'originalname' of undefined
  app.locals.bucket
    .file(req.file)
    .createWriteStream()
    .end(req.file.buffer)
    .then((i) => res.send("done"))
    .catch((e) => res.send(e));
});

Request in postman screenshot

原来 Firebase 和 multer 不能很好地结合在一起。我参考了这篇文章,它解释了为什么 multer 不起作用以及如何使用 Busboy 实现相同的目标。

https://mikesukmanowsky.com/firebase-file-and-image-uploads/