zoukankan      html  css  js  c++  java
  • node初学制作登录服务器实例

    文件目录如下:

     

    user.html代码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <title></title>
    </head>
    <body>
    <p>用户名:<input type="text" id="user"></p>
    <p>密码:<input type="password" id="pass"></p>
    <input type="button" value="注册" id="reg_btn">
    <input type="button" value="登录" id="login_btn">
    <script src="ajax.js" charset="utf-8"></script>
    <script type="text/javascript">
    window.onload=function(){
    var oTextUser=document.getElementById('user');
    var oTxtPass=document.getElementById('pass');
    var oBtnReg=document.getElementById('reg_btn');
    var oBtnLogin=document.getElementById('login_btn');

    oBtnReg.onclick=function(){
    ajax({
    url:'/user',
    data:{act:'reg',user:oTextUser.value,pass:oTxtPass.value},
    type:'get',
    success:function(str){
    var json=eval('('+str+')');

    if(json.ok){
    alert('注册成功');
    }
    else{
    alert('注册失败'+','+json.msg);
    };
    },
    error:function(){
    alert('通信失败');
    }
    })
    }
    oBtnLogin.onclick=function(){
    ajax({
    url:'/user',
    data:{act:'login',user:oTextUser.value,pass:oTxtPass.value},
    type:'get',
    success:function(str){
    var json=eval('('+str+')');

    if(json.ok){
    alert('登录成功');
    }
    else{
    alert('登录失败'+','+json.msg);
    };
    },
    error:function(){
    alert('通信失败');
    }
    })
    }
    }
    </script>
    </body>
    </html>

    server.js代码:

    var http=require('http');
    var fs=require('fs');
    var querystring=require('querystring');
    var urlLib=require('url');

    var users={};

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

    req.on('end',function(){
    var obj=urlLib.parse(req.url,true);
    var url=obj.pathname;
    var GET=obj.query;
    var POST=querystring.parse(str);
    //访问接口
    if(url=='/user'){
    switch(GET.act){
    case 'reg':
    //1.检查用户名是否已经有了
    if(users[GET.user]){
    res.write('{"ok":false,"msg":"此用户已存在"}');
    }
    else{
    users[GET.user]=GET.pass;
    res.write('{"ok":true,"msg":"注册成功"}');
    }
    break;
    case 'login':
    if (users[GET.user]==null) {
    res.write('{"ok":false,"msg":"此用户不存在"}');
    }
    else if(users[GET.user] != GET.pass){
    res.write('{"ok":false,"msg":"用户名或密码有误"}');
    }
    else{
    res.write('{"ok":true,"msg":"登录成功"}');
    }
    break;
    default:
    res.write('{"ok":false,"msg":"未知的act"}');
    }
    res.end();
    }
    else{
    //访问文件
    var file_name='./www'+url;
    fs.readFile(file_name,function(err,data){
    if(err){
    res.write('404');
    }
    else{
    res.write(data);
    }
    res.end();
    })
    }

    })
    })
    server.listen(8080);

  • 相关阅读:
    Python自学笔记(12day)
    Python自学笔记(11day)
    Python自学笔记(10day)
    Python自学笔记(9day)
    Python自学笔记(8day)
    form标签的使用
    form标签的使用法
    img标签的使用方法
    <a></a>标签的使用
    html的标签
  • 原文地址:https://www.cnblogs.com/fanfan0916/p/9524882.html
Copyright © 2011-2022 走看看