node.js写服务器

// 引入内置模块

var
http = require("http"); var fs = require("fs"); var url = require("url"); var path = require('path'); var querystring = require("querystring"); var server = http.createServer(); server.on("request",function(req,res){ //url路由转换 var urlObj = url.parse(req.url,true); console.log(req.url) //路径名 var pathname = urlObj.pathname; console.log(pathname); //路径携带的参数 var query = urlObj.query; // var extname = path.extname(req.url); // console.log(extname); // console.log(pathname); //静态伺服 if(pathname == "/register.html" && req.method == "GET"){ console.log("1111"); fs.readFile("./register.html","utf8",function(err,data){ res.end(data); }) } else if(pathname == "/jquery-1.11.3.min.js" && req.method == "GET"){ fs.readFile("./jquery-1.11.3.min.js","utf8",function(err,data){ res.end(data); }) } else if(pathname == "/login.html" && req.method == "GET"){ fs.readFile("./login.html","utf8",function(err,data){ res.end(data); }) } else if(pathname == "/shouye.html" && req.method == "GET"){ fs.readFile("./shouye.html","utf8",function(err,data){ res.end(data); }) } //注册用户名失焦验证 else if(pathname == "/register_blur" && req.method == "GET"){ fs.readFile("./login.json","utf8",function(err,data){ //将文件中存在的用户以json字符串格式转化成json对象 data = JSON.parse(data); var flag = true; for(var i = 0;i<data.length;i++){ //判断文件中是否存在注册的用户名,一旦找到一个存在的uname则停止寻找,返回结果 if(data[i].uname == query.uname){ flag = false; break; } } if(flag){ res.end("1") }else{ res.end("0") } }) } //登录时用户名失焦验证 else if(pathname == "/login_blur" && req.method == "GET"){ fs.readFile("./login.json","utf8",function(err,data){ //将文件中存在的用户以json字符串格式转化成json对象 data = JSON.parse(data); var flag = true; for(var i = 0;i<data.length;i++){ //判断文件中是否存在注册的用户名,一旦找到一个存在的uname则停止寻找,返回结果 if(data[i].uname == query.uname){ flag = false; break; } } if(flag){ res.end("1") }else{ res.end("0") } }) } //登录验证 else if(pathname == "/login" && req.method == "POST"){ var pathData = ""; req.on("data", function(chunk){ pathData += chunk; // console.log(pathData) }) req.on("end",function(){ var pathObj = querystring.parse(pathData); console.log(pathObj); if(pathObj.uname && pathObj.upassword){ fs.readFile("./login.json","utf8",function(err,data){ data = JSON.parse(data); var flag = false; for(var i = 0; i < data.length; i++){ if(data[i].uname == pathObj.uname && data[i].upassword == pathObj.upassword){ flag = true; break; } } if(flag){ //成功 res.end("1"); }else{ //用户名或密码错误 res.end("2"); } }) }else{ //参数错误 res.end("0"); } }) } //注册验证 else if(pathname == "/register" && req.method == "POST"){ var pathData = ""; req.on("data", function(chunk){ pathData += chunk; // console.log(pathData) }) req.on("end",function(){ var pathObj = querystring.parse(pathData); // console.log(pathObj); if(pathObj.uname && pathObj.upassword){ fs.readFile("./login.json","utf8",function(err,data){ data = JSON.parse(data); data.push(pathObj); var data_str = JSON.stringify(data); fs.writeFile("./login.json",data_str,"utf8",function(err){ res.end("1") }) }) }else{ //参数错误 res.end("0"); } }) } //首页获取数据 else if(pathname == "/index_getdata" && req.method == "GET"){ var uname = query.uname; fs.access('./data/' + uname + ".json",function(err){ if(err){ fs.writeFile('./data/' + uname + ".json",'[]',function(err){ fs.readFile('./data/' + uname + ".json",'utf8',function(err,data){ var queryData = "{'message':'OK','data':" + data + '}' res.end(queryData); }) }) }else{ fs.readFile('./data/' + uname + ".json","utf8",function(err,data){ var queryData = "{'message':'OK','data':" + data + '}' res.end(queryData); }) } }) } //添加数据 else if(pathname == "/add_data" && req.method == "POST"){ var pathData = ''; req.on('data', function(chunk){ pathData += chunk; }); req.on('end', function(){ var pathObj = querystring.parse(pathData); var uname = pathObj.uname; fs.readFile('./data/' + uname + ".json",'utf8',function(err,data){ var data = JSON.parse(data); if(data.length == 0){ var obj = { pid : 0, name: pathObj.name, sex : pathObj.sex, age : pathObj.age, mail : pathObj.mail, phone : pathObj.phone } data.push(obj); var str = JSON.stringify(data); fs.writeFile('./data/' + uname + ".json",str,function(err){ res.end('1'); }) }else{ var obj = { pid : data[data.length-1].pid + 1, name: pathObj.name, sex : pathObj.sex, age : pathObj.age, mail : pathObj.mail, phone : pathObj.phone } data.push(obj); var str = JSON.stringify(data); fs.writeFile('./data/' + uname + ".json",str,function(err){ res.end('1'); }) } }) }); } //删除数据 else if(pathname == "/remove_data" && req.method == "GET"){ var uname = query.uname; var pid = query.pid; fs.readFile('./data/' + uname + ".json",'utf8',function(err,data){ var data = JSON.parse(data); for(var i = 0;i < data.length;i++){ if(data[i].pid == pid){ break; } } data.splice(i,1); var str = JSON.stringify(data) fs.writeFile('./data/' + uname + ".json",str,function(err){ res.end("1"); }) }) } //修改数据 else if(pathname == "/modify_data" && req.method == "POST"){ var pathData = ''; req.on('data', function(chunk){ pathData += chunk; }); req.on('end', function(){ var pathObj = querystring.parse(pathData); var uname = pathObj.uname; var pid = pathObj.pid; fs.readFile('./data/' + uname + ".json",'utf8',function(err,data){ var data = JSON.parse(data); for(var i = 0;i < data.length;i++){ if(data[i].pid == pid){ break; } } if(pathObj.name){ data[i].name = pathObj.name } if(pathObj.sex){ data[i].sex = pathObj.sex } if(pathObj.age){ data[i].age = pathObj.age } if(pathObj.mail){ data[i].mail = pathObj.mail } if(pathObj.phone){ data[i].phone = pathObj.phone } var str = JSON.stringify(data); fs.writeFile('./data/' + uname + ".json",str,function(err){ res.end("1"); }) }) }) } }) server.listen(3808); console.log("服务器3808正在运行")
posted @ 2019-03-05 16:40  猝死边缘挣扎的狗子  阅读(155)  评论(0编辑  收藏  举报