body-parser中间件写法(已弃用)
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors');
const app = express();
//解决跨域
app.use(cors());
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json())
app.post('/service/update', (req, res) => {
console.log("req",req.body)
})
app.listen(3000, function () {
console.log('app is runing at port 3000');
})
替代方法
express最新版版内置了post参数解析,可以直接调用express
const express = require('express')
const cors = require('cors');
var app = express();
//解决跨域
app.use(cors());
/*
*配置body-parser中间件,用来解析POST请求体
*配置后,request请求对象就会多出一个body属性
*使用request.body就可以获取到POST请求的数据了
*/
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.post('/service/update', (req, res) => {
console.log("req",req.body)
})
app.listen(3000, function () {
console.log('app is runing at port 3000');
})