const http = require('http');
const url = require('url')
const querystring = require('querystring')
let user={admin:123456
}
http.createServer((req,res)=>{let path,get,post//如果是get请求if(req.method=='GET'){let{pathname,query} = url.parse(req.url,true)path=pathname,get=querycomplete()//如果是POST请求}else if(req.method=='POST'){let arr=[]req.on('data',buffer=>{arr.push(buffer)})req.on('end',()=>{post=querystring.parse(Buffer.concat(arr).toString())})complete()}function complete(){res.writeHead(200,{"Content-Type":"text/plain;charset=utf-8"})if(path=='/login'){let {username,password}=getif(!user[username]){res.end(JSON.stringify({err:1,msg:"用户不存在"}))}else if(user[username]!=password){res.end(JSON.stringify({err:1,msg:"密码错误"}))}else{res.end(JSON.stringify({err:0,msg:"登录成功"}))}}else if(path=='/reg'){}else{}}
}).listen(8887);
运行结果