建议httpserver搭建、get请求登录及注册实例

一,访问接口:get请求登录及注册

bug原因:(1)前台:ajax访问不可跨域,所以localhost:8089/user.html;

              (2)前台:因为后台返回的是json字符串,需要转化为json对象;

			  	//var data = eval('('+data+')');
			  	var data = jQuery.parseJSON(data);//由JSON字符串转换为JSON对象
				//var data = JSON.parse(data);

              (2)后台:存储用户信息的users,位置(放在server对象中的话,每次发送的数据时会清空之前的数据)

后台代码:

const http = require('http');
const fs = require('fs');
const querystring = require('querystring');
const urlLip = require('url');
var users = {};//存放用户数据

http.createServer(function(req,res){
	var str = '';
	req.on('data',function(data){
		str += data;
	});

	req.on('end',function(){
		var obj = urlLip.parse(req.url,true);
		var url = obj.pathname;
		const GET = obj.query;

		var POST = querystring.parse(str);

		var filePath = './www'+url;
		if(url == '/user'){
			//访问接口
			switch(GET.act){
				case 'reg':
						//注册
						//1.检查用户是否已经存在
					if(users[GET.userName]){
						res.write('{"ok":false,"msg":"该用户账号已经被注册"}')
					}else{
						//2.插入用户信息
						users[GET.userName] = GET.userPassword;
						console.log(users);
						res.write('{"ok":true,"msg":"注册成功"}')
					}
				break;
				case 'login':
					    //登录
					    //1.检查用户是否已经存在
					if(users[GET.userName]== null){
						res.write('{"ok":false,"msg":"该用户不存在"}')
					}else if(users[GET.userName] != GET.userPassword){
						//2.是否与存储的信息一致
						res.write('{"ok":false,"msg":"用户名或密码错误"}')
					}else{
						res.write('{"ok":true,"msg":"登录成功"}')
					}
				break;
				default:
					res.write('{"ok":false,"msg":"未知的act"}')
			};
			res.end();
		}else{
			//访问文件
			fs.readFile(filePath, function(err,data){
				if(err){
					res.write('404');
				}else{
					res.write(data);
				}
				res.end();
			});
		}
	})
}).listen(8090);

前台代码:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>注册/登录</title>
	<link rel="stylesheet" href="">
	<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
	用户名:
	<input type="text" id="userName"><br/>
	密码:
	<input type="password" id="userPassword"><br/>
	<input type="button" id="reg_btn" value="注册">
	<input type="button" id="log_btn" value="登录">

	<script type="text/javascript">
	//注册
		$('#reg_btn').click(function(event) {
			$.ajax({
			  type: "GET",
			  url: '/user',
			  data: {
			  	act:'reg',
			  	userName:$('#userName').val(),
			  	userPassword:$('#userPassword').val()
			  },
			  success: function(data){
			  	var data = eval('('+data+')');
			  	console.log(data);
			  	if(data.ok){
			  		alert('注册成功');
			  	}else{
			  		alert(data.msg);
			  	}
			  },
			  errer: function(){
			  	alert('链接错误');
			  }
			});
		});
	//登录
		$('#log_btn').click(function(event) {
			$.ajax({
			  type: "GET",
			  url: '/user',
			  data: {
			  	act:'login',
			  	userName:$('#userName').val(),
			  	userPassword:$('#userPassword').val()
			  },
			  success: function(data){
			  	var data = eval('('+data+')');
			  	console.log(data);
			  	if(data.ok){
			  		alert('登录成功');
			  	}else{
			  		alert(data.msg);
			  	}
			  },
			  errer: function(){
			  	alert('链接错误');
			  }
			});
		});
    </script>
</body>
</html>

二,系统模块

(1)Crypto  加密

(2)Events  事件

(3)Net  网络操作

(4)OS   操作系统

(5)Path 文件路径

(6)Stream   流加载

(7)Timers  定时器

三,自定义模块


猜你喜欢

转载自blog.csdn.net/qq_33828155/article/details/80940233