提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
以下内容为http的学习
<!DOCTYPE html>
<html lang="zh">
<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>
<script src="New Js File.js"></script>
</head>
<body>
<form method="get" action="http://localhost:8080">
用户名:<input type="text" name="userName">
密码:<input type="password" name="passWord">
<input type="submit" value="GET">
</form>
<hr>
<form method="post" action="http://localhost:8080">
用户名:<input type="text" name="userName">
密码:<input type="password" name="passWord">
<input type="submit" value="POST">
</form>
</body>
</html>
一、POST请求
1.1 req.on(data) :每次发送的数据
1.2 req.on(end):数据发送完成
const http = require('http')
const querystring = require('querystring')
// 创建服务器
const server = http.createServer((req, res) => {
// 发送post请求
let postVal = ''
// 每次发送出去的数据
req.on("data", (chunk) => {
postVal += chunk
})
// 数据发送完成,
req.on('data', () => {
let post = querystring.parse(postVal, true).query;
console.log(post);
res.end()
// "POST用户名:" + post.postUserName + "-- -- -" + "POST密码:" + post.postPassWord
})
// 发送get请求
const url = require('url')
const reqUrl = req.url
// 解析get请求头
res.writeHead(200, {
"Content-Type": "text/html;charset=utf8"
})
const getVal = url.parse(reqUrl, true).query;
console.log(getVal.getUserName);
res.end("GET用户名:" + getVal.getUserName + "-- -- -" + "GET密码:" + getVal.getPassWord)
})
// 服务器监听端口:8080
server.listen('8080')
二、GET请求
2.1 通过url路径获取请求数据
总结
以上就是今天要讲的内容。