GET請求的參數在URL中,在原生Node中,需要使用url模塊來識別參數字符串。在Express中,不需要使用url模塊了。可以直接使用req.方法來直接獲取。
app.get('/getFile', function (req, res) {
let comm = req.query
console.log(comm) //{ a: '100', b: '200' }
console.log(req.host)
console.log(req.url)
console.log(req.method)
})
POST 請求: 在Express 中 沒有內置獲取表單POST請求體的API,這里我們需要使用一個第三方的包 :body-parser
第一步:
npm install body-parser
1
第二步:使用
2.1 導包:
var bodyParser = require('body-parser');
1
配置body-parser,只要加入這個配置則在req的請求對象上會多出來一個屬性:body
也就是說可以直接通過req.body來獲取表單post的請求體的數據了
//處理POST請求的
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.post('/post', function (req, res) {
//通過req.body來獲取POST的提交的請求體數據
console.log(req.body)
})
————————————————
版權聲明:本文為CSDN博主「邪小新」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_42074075/java/article/details/89298033