Get the data requested by get
An API is built into Express, which can be accessed directly request.query
.
// 在express中可以直接通过 request.query 来获取字符串参数
// http://127.0.0.1:4000/about?name=zhangsan&message=hello
app.get('/about',function (request,response) {
console.log(request.query);
response.send('关于我');
})
Returned data format:{ name: 'zhangsan', message: 'hello' }
Get the data requested by the post
There is no built-in API for obtaining the post request body in Express, we need to use the third-party plug-in body-parser
installation npm install body-parser
Configuration body-parser
- Configuration code below, will be in the request on the request object will be out of a multi-attribute: body
- We can directly through
request.body
to get the data in the form POST request body
app.use(bodyParser.urlencoded({
extended: false }))
app.use(bodyParser.json())
use
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.urlencoded({
extended: false }))
app.use(bodyParser.json())
app.post('/about',function (request,response) {
console.log(request.body);
})
Reference
Express middleware body-parser
Further reading https://blog.csdn.net/weixin_43974265/category_10692693.html