//html部分
<body>
<form action="http://172.16.41.102:3000/" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
//js部分
const http = require("http")
const { URLSearchParams } = require("url")
http.createServer((req, res) => {
/* 添加请求头 */
res.setHeader("Content-Type", "text/html;charset=utf-8;")
/* 禁止图标响应 */
if (req.url == "/favicon.ico") {
res.end("");
return
}
/* 定义一个变量 */
let body = '';
/* 监听data事件,当有数据传输过来时,会自动触发。 */
req.on("data", chunk => {
/* 相加 */
body += chunk;
})
/* 监听end事件 */
req.on("end", () => {
/* 将查询字符解析为对像 */
let params = new URLSearchParams(body)
/*获取输入的用户名和密码 */
let username = params.get("username")
let password = params.get("password")
if (username == "zhangsan" && password == "123456") {
res.end("登录成功")
} else {
res.end("用户名或密码不正确")
}
})
}).listen(3000);
console.log("服务开启成功")
使用node实现简易的登录功能
最新推荐文章于 2024-05-03 20:59:12 发布