<!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="http://localhost:3000" method="post">
<p>
<input type="text" placeholder="请输入用户名" name="user">
</p>
<p>
<input type="password" placeholder="请输入密码" name="pwd">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</body>
</html>
//引入服务器http
const http = require("http");
//引入url请求地址
const url = require("url");
const querystring = require("querystring")
// post请求的数据 是添加到 请求体中
http.createServer((req, res) => {
//设置一个存储数据的容器
let body = "";
//监听data事件。有数据时触发
req.on("data", (chunk) => {
body += chunk;
})
//监听end事件 么有更多数据可读时触发
req.on("end", () => {
//传出的数据是字符串,要把它解析成对象
body = querystring.parse(body);
//设置请求头 200表示成功,后面的表示请求类型以及编码格式
res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" })
res.write(`欢迎 ${body.user}`)
res.end()
})
let query = url.parse(req.url, true).query
// console.log(query);
}).listen(3000, () => {
console.log("hello word");
})