JS Learning Summary----CRM client management System node Authoring API interface

Source: Internet
Author: User

The specific API code is as follows

varHTTP = require (' http '), URL= require (' URL '), FS= Require (' FS ');varServer = Http.createserver (function(req,res) {varUrlobj = Url.parse (Req.url,true), pathname=urlobj.pathname, Query= Urlobj.query;//stores information (and is stored as key-value pairs) After the question mark argument in the URL address of the client request .    //processing of static resource file requests    varReg =/\. (html| css| js| ICO)/i; if(reg.test (pathname)) {varsuffix = reg.exec (pathname) [1].touppercase (); varSuffixmime = "Text/html"; Switch(suffix) { Case"CSS": Suffixmime= "Text/css" Break;  Case"JS": Suffixmime= "Text/javascript" Break; }        Try{            varConfile = 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; }    //processing of API data interfaces    varCon =NULL, result =NULL, Customid =NULL, Custompath = "./json/custom.json"; //first, get the contents of the Custom.json file tocon = Fs.readfilesync (Custompath, ' utf-8 ')); Con.length===0?con = ' [] ':NULL;//in order to prevent us from custom.json nothing, con is an empty string, we use Json.parse when the conversion is error, we let it equals ' [] 'Con =json.parse (con); //1) Access to all customer information    if(Pathname = = = "/getlist"){        //start preparing the data returned to the client according to the specifications in the API documentationresult ={code:1, msg:"No Customer Information", Data:NULL        }; if(con.length>0) {result={code:0, msg:Success, Data:con}; } res.writehead (200,{' content-type ': ' Application/json;charset=utf-8; '});        Res.end (json.stringify (result)); return; }    //2), according to the customer ID passed in to obtain a specific customer information    if(pathname=== "/getinfo"){        //ID to pass the client in        varCustomid = query[' id ']; Result={code:1, msg:"Customer does not exist", Data:NULL        }         for(vari = 0;i<con.length;i++){            if(con[i]["id"]==Customid) {Result={code:0, msg:Success, Data:con[i]} Break; }} res.writehead (200,{' content-type ': ' Application/json;charset=utf-8; '});        Res.end (json.stringify (result)); return; }    //3), delete this customer based on the customer ID passed in    if(pathname=== "/removeinfo"){        varFlag =false; Customid= query["id"];  for(vari = 0;i<con.length;i++){            if(con[i]["id"]==Customid) {Con.splice (i,1); Flag=true;  Break; }} result={code:1, msg:' Delete Failed '        }        if(flag) {Fs.writefilesync (Custompath,json.stringify (con),' Utf-8 ') Result={code:0, msg:' Delete succeeded '}} res.writehead (200,{' content-type ': ' Application/json;charset=utf-8; '});        Res.end (json.stringify (result)); return; }    //4), increase customer information    if(pathname=== "/addinfo"){        //gets the content that the client passes in through the request principal        varstr = ""; 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:"Increased failure, no more information needed to be passed"                })); return; }            vardata =json.parse (str); //append an ID to the existing data: Gets the ID of the last item in Con, the new ID is added on the original basis, and if none of the previous items, our ID of this item is 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:"Increased success"            })); })        return; }    //5), modify customer information    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:"The modification failed, and no information was passed that required modification"                })); return; }            varFlag =false; Data=json.parse (str);  for(vari = 0;i<con.length;i++){                if(con[i]["id"]==data["id"]) {Con[i]=data; Flag=true;  Break; }} result.msg= "The modification failed, the customer needs to be modified does not exist"; if(flag) {Fs.writefilesync (Custompath,json.stringify (con),"Utf-8"); Result={code:0, msg:"Modified successfully"}} res.writehead (200,{' content-type ': ' Application/json;charset=utf-8; '});        Res.end (json.stringify (result)); })        return; }    //If the requested address is not one of these, the prompt does not existRes.writehead (404,{' content-type ': ' Text/plain;charset=utf-8; '}); Res.end ("The requested data interface does not exist")}) Server.listen (80,function() {Console.log ("Server is success,listening on port")})

JS Learning Summary----CRM client management System node Authoring API interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.