html 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>05-http_request</title>
</head>
<body>
<fieldset>
<legend><h2>表单1-get请求</h2></legend>
<form action="http://127.0.0.1:8765/get" method="get">
<input type="text" placeholder="姓名" name="name">
<input type="text" placeholder="年龄" name="age">
<input type="submit">
</form>
</fieldset>
<fieldset>
<legend><h2>表单2-css文件请求</h2></legend>
<form action="http://127.0.0.1:8765/css" method="get">
<input type="submit" value="请求css文件">
</form>
</fieldset>
<fieldset>
<legend><h2>表单3-post请求</h2></legend>
<form action="http://127.0.0.1:8765" method="post">
<input type="text" placeholder="用户名" name="username">
<input type="text" placeholder="密码" name="password">
<input type="submit" value="登录">
</form>
</fieldset>
<fieldset>
<legend><h2>表单4-请求img图片</h2></legend>
<form action="http://127.0.0.1:8765/img" method="get">
<input type="submit" value="请求图片">
</form>
</fieldset>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a52968ed1cf400c1df1595f9bce3023f.png)
js页面
const http = require("http");
const qs = require("querystring");
const url = require("url");
const fs = require("fs");
http.createServer(function (req, res) {
if (req.method == "GET") {
const urlObj = url.parse(req.url, true);
switch (urlObj.pathname) {
case "/get": {
res.writeHead(200, {"Content-Type":"text/plain; charset=utf-8"});
if (urlObj.query.name == "杜甫" && urlObj.query.age == "50") {
res.end("注册成功");
} else {
res.end("注册失败");
}
break;
}
case "/css": {
res.writeHead(200, {"Content-Type":"text/html; charset=utf-8"});
res.write("<link rel='stylesheet' type='text/css' href='http://127.0.0.1:8765/myStyle'/>");
res.end();
break;
}
case "/myStyle": {
let readS = fs.createReadStream("node02.css");
readS.pipe(res);
break;
}
case "/img": {
res.writeHead(200, {"Content-Type":"text/html; charset=utf-8"});
res.write("<img src='http://127.0.0.1:8765/img1'/>");
res.end();
break;
}
case "/img1": {
let readS = fs.createReadStream("max.jpg");
readS.pipe(res);
}
case "/favicon.ico": {
let readS = fs.createReadStream("favicon.ico");
readS.pipe(res);
break;
}
}
} else if (req.method == "POST") {
res.writeHead(200, {"Content-Type":"text/html; charset=utf-8"});
let allData = "";
req.on("data", function (chunk) {
allData += chunk;
});
req.on("end", function () {
let queryObj = qs.parse(allData);
if (queryObj.username == "张三" && queryObj.password == "111") {
res.write("登陆成功");
} else {
res.write("登录失败");
}
res.end();
});
}else {
res.end();
}
}).listen(8765, function () {
console.log("开启监听8765...");
});