zoukankan      html  css  js  c++  java
  • js学习总结----crm客户管理系统之node编写API接口

    具体API代码如下

    var http = require('http'),
        url = require('url'),
        fs = require('fs');
    var server  = http.createServer(function(req,res){
        var urlObj = url.parse(req.url,true),
            pathname = urlObj.pathname,
            query = urlObj.query;//存储的是客户端请求的URL地址中的问号传参后面的信息(并且是以键值对的方式存储的)
        //静态资源文件请求的处理
        var reg = /.(HTML|CSS|JS|ICO)/i;
        if(reg.test(pathname)){
            var suffix = reg.exec(pathname)[1].toUpperCase();
            var suffixMIME = "text/html";
            switch(suffix){
                case "CSS":
                    suffixMIME = "text/css"
                    break;
                case "JS":
                    suffixMIME = "text/javascript"
                    break;
            }
            try{
                var conFile = fs.readFileSync("."+pathname,'utf-8');
                res.writeHead(200,{'content-type':suffixMIME+";charset=utf-8"});
                res.end(conFile);
            }catch(e){
                res.writeHead(404,{'content-type':"text/plain;charset=utf-8"});
                res.end("file is not found~");
            }
            return;
        }
        //API数据接口的处理
        var con = null,result = null,customId = null,customPath = "./json/custom.json";
        //首先把custom.json文件中的内容都获取到
        con = fs.readFileSync(customPath,'utf-8');
        con.length===0?con = '[]':null;//为了防止我们custom.json什么都没有,con是一个空字符串,我们使用JSON.parse转换的时候会报错,我们让其等于'[]'
        con = JSON.parse(con);
        //1)获取所有的客户信息
        if(pathname === "/getList"){
            //开始按照API文档中的规范准备给客户端返回的数据
            result = {
                code:1,
                msg:"没有任何的客户信息",
                data:null
            };
            if(con.length>0){
                result = {
                    code:0,
                    msg:"成功",
                    data:con
                };
            }
            res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
            res.end(JSON.stringify(result));
            return;
        }
        //2)、根据传递进来的客户ID获取某一个具体的客户信息
        if(pathname==="/getInfo"){
            //把客户端传递进来的ID
            var customId = query['id'];
            result = {
                code:1,
                msg:"客户不存在",
                data:null
            }
            for(var i = 0;i<con.length;i++){
                if(con[i]["id"]==customId){
                    result = {
                        code:0,
                        msg:"成功",
                        data:con[i]
                    }
                    break;
    
                }
    
            }
            res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
            res.end(JSON.stringify(result));
            return;
    
        }
        //3)、根据传递进来的客户ID删除这个客户
        if(pathname==="/removeInfo"){
            var flag = false;
            customId = query["id"];
            for(var i = 0;i<con.length;i++){
                if(con[i]["id"]==customId){
                    con.splice(i,1);
                    flag = true;
                    break;
                }
            }
            result={
                code:1,
                msg:'删除失败'
            }
            if(flag){
                fs.writeFileSync(customPath,JSON.stringify(con),'utf-8')
                result={
                    code:0,
                    msg:'删除成功'
                }
            }
            res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
            res.end(JSON.stringify(result));
            return;
        }
        //4)、增加客户信息
        if(pathname==="/addInfo"){
            //获取客户端通过请求主体传递进来的内容
            var str = "";
            req.on("data",function(chunk)){
                str+=chunk;
            }
            req.on("end",function(){
                if(str.length===0){
                    res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
                    res.end(JSON.stringify({
                        code:1,
                        msg:"增加失败,没有传递任何需要增加的信息"
                    }));
                    return;
                }
                var data = JSON.parse(str);
                //在现有的DATA中追加一个ID:获取con中最后一项的ID,新的ID是在原有基础上加一即可,如果之前一项都没有,我们这一项的ID就是1
                if(con.length===0){
                    data["id"] = 1;
                }else{
                    data["id"] = parseFloat(con[con.length-1]["id"])+1;
                }
                con.push(data);
                fs.writeFileSync(customPath,JSON.stringify(con),'utf-8');
                res.end(JSON.stringify({
                    code:0,
                    msg:"增加成功"
                }));
                
            })
            return;
        }
        //5)、修改客户信息
        if(pathname==="/updateInfo"){
            str ="";
            req.on("data",function(chunk){
                str+=chunk;
            })
            req.on("end",function(){
                if(str.length===0){
                    res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
                    res.end(JSON.stringify({
                        code:1,
                        msg:"修改失败,没有传递任何需要修改的信息"
                    }));
                    return;
                }
                var flag = false;
                    data = JSON.parse(str);
                for(var i = 0;i<con.length;i++){
                    if(con[i]["id"]==data["id"]){
                        con[i] = data;
                        flag = true;
                        break;
                    }
                }
                result.msg = "修改失败,需要修改的客户不存在";
                if(flag){
                    fs.writeFileSync(customPath,JSON.stringify(con),"utf-8");
                    result = {
                        code:0,
                        msg:"修改成功"
                    }
                }
                res.writeHead(200,{'content-type':'application/json;charset=utf-8;'});
                res.end(JSON.stringify(result));
            })
            return;
        }
        //如果请求的地址不是上述任何一个,则提示不存在
        res.writeHead(404,{'content-type':'text/plain;charset=utf-8;'});
        res.end("请求的数据接口不存在")
    
    })
    server.listen(80,function(){
        console.log("server is success,listening on 80 port")
    })
  • 相关阅读:
    Nginx常用命令
    Nginx进程模型
    华为联运游戏或应用审核驳回:使用花币充值时,对支付延时处理不当,导致商品不到账
    使用Data Ability读取系统设置项
    100个HarmonyOS 2.0开发者Beta公测名额,限时认领!
    【有奖活动】HarmonyOS开发者创新大赛颁奖典礼丨见证星星之火燃爆盛夏
    你其实可以更快!用Toolkit拖拽式编码方法帮你快速提升开发效率
    真机调试设备不够?华为AGConnect云调试帮你忙
    华为后台某应用商品展示价格币种随其他应用配置而变化
    HMS Core.Sparkle影音娱乐创新沙龙邀您参加
  • 原文地址:https://www.cnblogs.com/diasa-fly/p/7274311.html
Copyright © 2011-2022 走看看