node.js 2 http

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

以下内容为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路径获取请求数据


总结

以上就是今天要讲的内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值