node.js编写的登录验证

warning: 这篇文章距离上次修改已过263天,其中的内容可能已经有所变动。

客户端通过表单post请求发送登录数据,后端收取验证并且跳转页面。
运行于 http://127.0.0.1:5000/login.html

jt.pngjt.png

const fs = require("fs");//使用require函数导入fs模块,使用const储存于fs
const http = require("http");//使用require函数导入http模块,使用const储存于http
const qs = require("querystring");
const port = 5000;
const ip = "127.0.0.1";
const sendResponse = (filename, statusCode, response) => {//用来读取和发送html源代码
    fs.readFile(./${filename}, (error, data) => {//读取html文件
        if (error) {//判断是否成功读取文件
            response.statusCode = 500;//设置错误码
            response.setHeader("Content-Type", "text/plain");//设置返回格式
            response.end("Sorry, internal error");//使用end返回内容
        }else{
            response.statusCode = statusCode;
            response.setHeader("Content-Type", "text/html");
            response.end(data);
        }
    })
}
const server = http.createServer( (request, response) => {//使用http模块内的createServe来创建服务器并储存于server,并在服务器内放入请求监听器
    console.log(request.url, request.method);//识别客户端输入的url和请求方法
    const method = request.method;
    let url = request.url;
    if (method === "GET"){//判断请求是否为GET
        const requestUrl = new URL(url, http://${ip}:${port});//使用URL object获取客户端url
        console.log(requestUrl);
        console.log(requestUrl.searchParams.get("lang"));
        url = requestUrl.pathname;//获取路径名
        const lang = requestUrl.searchParams.get("lang");//创建lang并给值
        let selector;
        if (lang === null|| lang === "en"){//判断lang值,分配到不同页面
            selector = "";
        }else if(lang === "zh"){
            selector = "-zh";
        }else{
            selector = "";
        }
        if (url === "/"){
            sendResponse(index${selector}.html, 200, response);
        } else if (url === "/about.html"){
            sendResponse(about${selector}.html, 200, response);
        } else if (url === "/login.html"){
            sendResponse(login${selector}.html, 200, response);
        } else if (url === "/login-success.html"){
            sendResponse(login-success${selector}.html, 200, response);
        } else if (url === "/login-fail.html"){
            sendResponse(login-fail${selector}.html, 200, response);
        } else {
            sendResponse(404${selector}.html, 404, response);
        }
        } else {//非GET请求
        if(url === "/process-login"){
            let body = [];//创建空array用于储存从客户端来的内容
            request.on("data", (chunk) => {//请求监听器读取data,存储读取数据于chunk
                body.push(chunk);//将chunk内的数据交给body
            });
            request.on("end", () => {
                body = Buffer.concat(body).toString();//将Buffer数据转换为可读取数据
                body = qs.parse(body);//使用qs.parse将可读数据分段
                console.log(body);
                if(body.username === "mio" && body.password === "akiyama"){
                    response.statusCode = 301;
                    response.setHeader("Location", "/login-success.html")
                }else{
                    response.statusCode = 301;
                    response.setHeader("Location", "/login-fail.html")
                }
                response.end();
            });
        }
    }
});
server.listen(port, ip, () => {//使用sever Object里的listen函数监听来自前端的请求
    console.log(Server is running at http://${ip}:${port})
});

login.rar

none
最后修改于:2021年10月08日 14:01

添加新评论

召唤看板娘