express-post 请求

1.导入body-parser

yarn add body-parser

2、配置

const bodyParser=require('body-parser')
const app = express()
app.use(bodyParser.json())//可以处理json
// app.use(bodyParser.urlencoded({ extended: false }))  //只能处理urlencoded
app.post('/',function(req,res){
  console.dir(req.body)
  res.send('ok')
})

灵活处理urlencoded和jison

//灵活处理
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/post/a',jsonParser,function(){
  console.log(req.body)
  send.send('hell0')
})
app.post('/post/b',urlencodedParser,function(){
  console.log(req.body)
  send.send('hello')
})

 

posted on 2020-11-27 10:36  秃了头也不退休  阅读(194)  评论(0编辑  收藏  举报

导航