<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="login" method="post">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="Sign in">
</form>
</body>
</html>
const express = require("express");
const app = express();
app.listen(8080);
//引入querystring,将字符串转换成对象
const querystring = require("querystring");
//调转到登录页面
app.get("/login.html",(req,res)=>{
console.log(__dirname+"/static/login.html");
res.sendFile(__dirname+"/static/login.html");
})
//post请求,获取post请求中的数据
app.post("/login",(req,res)=>{
req.on("data",myData=>{
console.log(querystring.parse(myData.toString()));
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="reg" method="post">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="Sign in">
</form>
</body>
</html>
const express = require("express");
const app = express();
app.listen(8080);
//处理req骑牛携带数据的中间件,将req的数据局放在req.body的属性上
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended:true
}))
//调转到注册页面
app.get("/reg.html",(req,res)=>{
res.sendFile(__dirname+"/static/reg.html");
})
//第二种方式获取post请求中的数据
app.post("/reg",(req,res)=>{
//获取post请求参数
const {username,password} = req.body;
console.log(username,password);
})