如何处理来自 express 4 的 FormData [英] How to handle FormData from express 4
本文介绍了如何处理来自 express 4 的 FormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试向我的节点服务器发送一些表单数据,但是 req.body
在节点端没有我的表单字段
I tried sending some form data to my node server but req.body
has none of my form fields the node side
var express = require('express')
var app = express()
var path = require('path')
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function (req, res) {
res.sendFile('index.html')
})
app.post('/sendmail', function (req, res) {
const formData = req.body.formData
这是我从浏览器发送的内容
this is what I'm sending from the browser
fetch('/send', {
method: 'POST',
body: new FormData(form)
})
在开发工具中,我只能看到在 Referer 中传递的数据,这可能是我的问题
in dev tools I only see the data passed in the Referer, maybe that is my issue
Referer:http://localhost:3000/?name=&budget=%C2%A31000
推荐答案
body-parser
不处理多部分主体,这就是 FormData
提交的内容.
body-parser
doesn't handle multipart bodies, which is what FormData
is submitted as.
相反,使用像 multer
这样的模块.
Instead, use a module like multer
.
例如,检索请求的(常规)字段:
For example, to retrieve the (regular) fields of a request:
const multer = require('multer');
const upload = multer();
app.post('/send', upload.none(), (req, res) => {
const formData = req.body;
console.log('form data', formData);
res.sendStatus(200);
});
这篇关于如何处理来自 express 4 的 FormData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文