不能在heroku上用Node.js做多个名称字段的POST

Can't do multiple name-field POST with Node.js on heroku

本文关键字:字段 POST heroku 上用 Node js 不能      更新时间:2023-09-26

我正在开发一个web应用程序与Node.js。我用enctype="multipart/form-data"写了一个隐藏的表单来上传文件和一些文本,但奇怪的是,我只能在herokuapp服务器上获得一个字段(表单中的名称),而不是我想要的多个字段。

客户端代码如下:
<form action="/target" method="post" enctype="multipart/form-data">
   <input hidden type="file" name="file" id="file">
   <input hidden type="text" name="A" id="A" value="AAA">
   <input hidden type="text" name="B" id="B" value="BBB">
   <input hidden type="text" name="C" id="C" value="CCC">
   <input hidden type="submit" value="Submit">
</form>

服务器端代码如下:

app.post('/target', function(req, res, next) {
    console.log(req.files.file.path);
    console.log(req.body.A);
    console.log(req.body.B);
    console.log(req.body.C);
    res.end();
})

我只能得到req.body.C与其他2剩余的undefined,但它完全正常,当我在本地主机上运行。
我使用multer来处理多部分表单,配置:

app.use(multer({dest:'./public/uploads/'}))

请使用环境变量NODE_ENV=production在本地运行服务器。这将像在生产环境中运行服务器一样运行服务器。然后尝试重现此问题。

另一件要考虑的事情是您使用的是哪个Express版本。Express 4不再提供正文解析器中间件;你必须自己把它带来。

为提出建议作为回答而道歉。我还没有足够的声望来发表评论

Heroku有一个只读文件系统。可写入目录为。/tmp或。/log。祝你好运!