zoukankan      html  css  js  c++  java
  • Node简单服务器开发

    运用的知识:http,fs,get,post

    接口定义:
    /user?act=reg$user=aaa&pass=bbb
    后台返回格式:{"ok":false,"msg":"原因"}
    /user?act=login$user=aaa&pass=bbb
    后台返回格式:{"ok":true,"msg":"原因"}

    前端访问:
    对文件的访问:
    http://localhost:8080/1.html
    http://localhost:8080/ajax.js
    http://localhost:8080/1.jpg

    对接口的访问:
    http://localhost:8080/user?act=reg$user=aaa&pass=bbb

    服务器(后端代码):
    const http = require("http");
    const querystring = require("querystring");
    const fs = require("fs");
    const urlLib = require("url");

    var users = {}; //定义用户json,比如{“blue”:“123456”,“zhangsan”:“666666”}

    var server = http.createServer(function(req,res){
    var str = "";
    req.on("data",function(data){
    str += str;
    });
    req.on("end",function(){
    var obj = urlLib.parse(req.url,true);
    const url = obj.pathname;
    const GET = obj.query;
    const POST = querystring.parse(str);

    //区分一下前端到底访问接口还是文件
    if (url == '/user') { //访问接口
    switch (GET.act){
    case 'reg':
    //检查前端传过来的参数用户名是否已经有了
    if (users[GET.user]) {
    res.write('{"ok":false,"msg":"此用户已存在"}')
    } else{
    //有的话往users里面插入
    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();
    });
    }
    });
    }).listen(8081);

    前端代码:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="ajax.js" ></script>
    <script>
    window.onload=function(){
    var oTxtUser = 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:oTxtUser.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:'reg', user:oTxtUser.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>
    </head>
    <body>
    用户名: <input type="text" id="user"/><br />
    密码:<input type="password" id="pass"/><br />
    <input type="button" id="reg_btn" value="注册" />
    <input type="button" id="login_btn" value="登录" />
    </body>
    </html>

  • 相关阅读:
    配置navigation bar外观
    加急审核
    UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
    打开某个好友的聊天界面
    ALAssets的两种用法
    更改appstore开发商名字
    回调函数的使用
    相应字体所占的位置大小
    IOS的变量前加extern和static字段
    iOS开发代码规范(通用)
  • 原文地址:https://www.cnblogs.com/fengyongxian/p/8900662.html
Copyright © 2011-2022 走看看