There is no API provided in express to get the post request body data, so a plug-in ( body-parser
) is needed to get it
-
installation
npm install --save body-parser
-
Configure and use
let express = require('express'); // 引入包 let bodyParser = require('body-parser'); let app = express(); //配置body-parser // 只要加上这个配置,则在req请求对象上会多出来一个属性:body // 因此可以直接通过req.body来获取表单post请求体数据 app.use(bodyParser.urlencoded({ extended: false})); app.use(bodyParser.json()); app.post('/post',(req,res) => { // 通过req.body来获取表单post请求体数据 res.send(req.body); })