get和post是HTTP协议中的两种发送请求的方法

HTTP是基于TCP/IP的关于数据如何在万维网中通信的协议

一、get请求与querystring

get请求即客户端向server服务端请求数据,如:获取文章列表的分页码等

通过queryString来获取数据,如:liet.html” />
密码:

const http = require('http');const querystring = require('querystring');let users = {'admin': 12345, 'Tom': 123};const server = http.createServer((req, res)=>{    res.writeHead(200, {        'Access-Control-Allow-Origin': '*',        'Content-Type': 'application/json;charset=utf-8'    });    let postData = '';    req.on('data', chunk => {        postData += chunk;    })    req.on('end', ()=>{        if(req.url != '/favicon.ico'){            // console.log(postData)            let obj = querystring.parse(postData);            // console.log(obj.username);            let username = obj.username;            let password = obj.password;            let act = obj.act;            console.log(req.url)            if(req.url == '/user1'){                switch(act){                    case 'reg':                        if(users[username]){                            res.write('{"flag": false, "msg": "此账号已存在!"}');                        }else{                            users[username] = password;                            res.write('{"flag": true, "msg": "注册成功!"}');                        }                        break;                    case 'login':                        if(users[username] == null){                            res.write('{"flag": false, "msg": "此账号不存在!"}');                        }else if(users[username] !== password){                            res.write('{"flag": false, "msg": "密码错误!"}');                        }else {                            res.write('{"flag": true, "msg": "登录成功!"}');                        }                        break;                }            }        }        res.end();    })});server.listen(3000, ()=>{});
const http = require('http');let users = {'admin': 12345, 'Tom': 123};const server = http.createServer((req, res)=>{    if(req.url != '/favicon.ico'){        let obj = new URL(req.url, 'http://127.0.0.1/');        let username = obj.searchParams.get('username');        let password = obj.searchParams.get('password');        let act = obj.searchParams.get('act');        res.writeHead(200, {            // 只针对某个地址实现跨域            // 'Access-Control-Allow-Origin': 'http://127.0.0.1:5500',            // 所有请求都允许跨域            'Access-Control-Allow-Origin': '*',            'Content-Type': 'application/json;charset=utf-8'        });        let url=req.url.split('?')[0];        if(url == '/user'){            switch(act){                case 'reg':                    if(users[username]){                        res.write('{"flag": false, "msg": "此账号已存在!"}');                    }else{                        users[username] = password;                        res.write('{"flag": true, "msg": "注册成功!"}');                    }                    break;                case 'login':                    if(users[username] == null){                        res.write('{"flag": false, "msg": "此账号不存在!"}');                    }else if(users[username] !== password){                        res.write('{"flag": false, "msg": "密码错误!"}');                    }else {                        res.write('{"flag": true, "msg": "登录成功!"}');                    }                    break;            }        }    }    res.end();});server.listen(3000, ()=>{});