nodejs处理用户的get请求
<!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="get">
<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");
// get请求的数据是拼接在url地址后边,以 ? 作为连接, 多个数据之间 使用 & 符号连接
http.createServer((req, res) => {
// 处理用户get的请求 传递的数据 在query字段中
let query = url.parse(req.url, true).query
// console.log(query);
//设置请求头 200表示成功,后面的表示请求类型以及编码格式
res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8" })
//判断用户输入的密码或用户名是否正确
let acont = {
name: "盖伦",
pwd: "123456"
}
if (query.user == acont.name) {
if (query.pwd == acont.pwd) {
res.write("欢迎登录")
res.end();
} else {
res.write("用户名或密码错误")
res.end();
}
} else {
res.write("用户名或密码错误")
res.end();
}
}).listen(3000, () => {
console.log("hello word");
})