使用习惯了get请求,突然要使用post的json卡住了。咱们看看如果从post请求中读取到json的参数。
安装 ‘body-parser’ 库
npm install body-parser
使用时直接配置下即可:
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
//读取json配置
app.use(bodyParser.json({ limit: '1mb' }))
app.use(bodyParser.urlencoded({
extended: true
}))
app.post('/query', (req, res) => {
res.send(req.body)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
读取参数:req.body
这样就可以愉快的读取到前端传过来的参数啦。