第一步,下载这个第三方模块: npm install body-parser
第二步:
编写获取post参数的程序:
const express=require('express')
const bodyParser=require('body-parser')
const app=express()
//拦截所有的请求,对post请求做出处理,把参数存在req.body中
//extended:false 方法内部使用querysyring模块处理请求参数的格式
app.use(bodyParser.urlencoded({extended:false}))
app.post('/add',(req,res)=>{
//接收post请求参数,
res.send(req.body)
})
app.listen(3000)
第三步:
编写html页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="http://localhost:3000/add" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" name="">
</form>
</body>
</html>
点击提交后,页面跳转至:http://localhost:3000/add