web开发概述(08):参数传递和获取—示例

登录验证 username:admin password:123456

myserver.js

/*
    登录验证:admin,123456
 */
const http = require('http');
const url = require('url');
const querystring = require('querystring');

http.createServer((req,res) =>{

    if(req.url.startsWith('/login') === false ){
        res.writeHead(500,{
            'Content-type':'text/plan;charset=utf8'
        });
        res.end("访问错误!");
        return;
    }

    if(req.method === 'GET'){
        let obj = url.parse(req.url,true);
        if(obj.query.username === 'admin' && obj.query.password === '123456'){
            res.end("GET.success!")
        }else{
            res.end("GET.fail!");
        }
    }

    if(req.method === 'POST'){
        let ddata = '';
        req.on('data',(chunk) => {
           ddata += chunk;
        });

        req.on('end',()=>{
                let obj =  querystring.parse(ddata);
                if (obj.username === 'admin' && obj.password === '123456' ){
                    res.end("POST.success!")
                }else{
                    res.end("POST.fail!");
                }
            }
        )
    }
}).listen(3000,() =>{
    console.log("服务启动ฅʕ•̫͡•ʔฅ");
    }
);

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录验证</title>
</head>
<body>
    <form action="http://localhost:3000/login"  method="post">
        用户名:<input type="text" name="username">
        密码:<input type="password" name="password">
        <input type="submit" value="登录">
    </form>
</body>
</html>
posted @ 2020-10-14 14:24  mrtransition  阅读(80)  评论(0)    收藏  举报