我如何读取在Node服务器上以application / x-www-form-urlencoded格式接收到的数据?

我正在接收webhookurl上的数据作为POST请求。 请注意,这个请求的内容types是application/x-www-form-urlencoded

这是一个服务器到服务器的请求。 而在我的节点服务器上,我试图通过使用req.body.parameters读取收到的数据,但结果值是“未定义”

那我怎么读取数据请求数据? 我需要parsing数据吗? 我是否需要安装任何npm模块? 你能写一个解释案例的代码片段吗?

如果您使用Express.js作为Node.js Web应用程序框架,那么使用ExpressJS body-parser 。

示例代码将是这样的。

 var bodyParser = require('body-parser'); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies // With body-parser configured, now create our route. We can grab POST // parameters using req.body.variable_name // POST http://localhost:8080/api/books // parameters sent with app.post('/api/books', function(req, res) { var book_id = req.body.id; var bookName = req.body.token; //Send the response back res.send(book_id + ' ' + bookName); });