Express.js使用FormData对象获取API

我在视图上有下面的代码

var fd = new FormData(); fd.append("hello", "world"); fetch('/vision', { method: 'post', "content-type": "application/json; charset=utf-8", body: JSON.stringify({ hello: "world" }) }) .then(data => { debugger; }) 

并采取行动来处理

 visionRouter.post("/", (req, res) => { vision.detectText(imageUrl, (err, text) => { res.send(text); }) }); 

路线正在被击中,但是req.body不存在。 你如何使用快速获取和FormData?

您需要使用body-parser节点模块。 像这样安装它

 npm install body-parser 

然后在你的代码中做到这一点

 var bodyParser = require("body-parser"); visionRouter.use(bodyParser.json()); 

现在你的req.body将能够访问你发送的JSON数据