美文网首页
app 服务器的简单搭建 增删数据

app 服务器的简单搭建 增删数据

作者: 越天高 | 来源:发表于2020-08-06 15:37 被阅读0次

    服务器代码,主要用来处理用户登陆,用户增加数据 和删除数据

    var express = require('express')
    var server = express();
    
    
    //引入bodyParser
    var bodyParser = require('body-parser')
    // 解析www格式post参数解析器
    var urlEncodedParser = bodyParser.urlencoded({extended:true})
    
    //使用中间键进行解析普通请求参数
    server.use('/addRoom',urlEncodedParser)
    
    
    //响应添加房间服务
    var roomList = []
    
    
    //响应登陆服务
    server.get('/login',function(req,res)
    {
        console.log('有客户端登陆'+ roomList)
        res.send(roomList)    
    })
    
    
    function arrContainrValue(arr, value)
    {
    
        for (var i=0; i< arr.length; i++)
        {
            var room = arr[i]
            if (room.roomID == value)
            {
                console.log('有这个值')
                return true
            }
        }
         return false
    
    
    }
    
    
    server.post('/addRoom',function(req,res)
    {
        console.log(req.body)
        var roomDic = req.body
        var isHasRoom = arrContainrValue(roomList, roomDic.roomID)
        if (isHasRoom == false)
        {
            roomList.push(roomDic)
            res.send(roomDic)
            console.log('创建一个新的房间')
        }
    
    })
    
    //移除直播间,更改路由
    server.get('/removeRoom',function(req,res)
    {
        console.log('监听到移除操作'+ ':'+ req.query.roomID)
        var isHasRoom = arrContainrValue(roomList, req.query.roomID)
        console.log(isHasRoom)
        if(isHasRoom == true)
        {
            var index = arrContainrValueIndex(roomList, roomList, req.query.roomI)
            roomList.splice(index,1)
            res.send({'isRemove':true})
        }else
        {
            res.send('room不存在')
        }
    
    })
    
    function arrContainrValueIndex(arr, value)
    {
    
        for (var i=0; i< arr.length; i++)
        {
            var room = arr[i]
            if (room.roomID == value)
            {
                return i
            }
        }
    
    }
    
    server.listen(8080,'192.168.99.158')
    
    console.log('开启监听')
    

    客户端简单搭建,利用服务器响应刷新页面

    • 增加数据
    - (IBAction)addLiveRoom:(id)sender
    {
    
        RoomItem *room = [RoomItem room];
        
        AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
        
        AFHTTPResponseSerializer *serialer = [AFHTTPResponseSerializer serializer];
        serialer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        mgr.responseSerializer =  serialer;
          
          
        [mgr POST:@"http://192.168.99.158:8080/addRoom" parameters:room.mj_keyValues headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
        {
            RoomItem *newRoom = [RoomItem mj_objectWithKeyValues:responseObject];
            if (newRoom)
            {
                [self.rooms addObject:newRoom];
                [self.tableView reloadData];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
        {
            NSLog(@"错误%@", error);
              
        }];
    }
    
    • 删除数据
    
        AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
        
        //会帮我们处理返回结果,
        AFJSONResponseSerializer *serialer = [AFJSONResponseSerializer serializer];
        serialer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        mgr.responseSerializer =  serialer;
        RoomItem *item = self.rooms[indexPath.row];
        NSDictionary *paragemter = item.mj_keyValues;
        [mgr GET:@"http://192.168.99.158:8080/removeRoom" parameters:paragemter headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
        {
            NSLog(@"%@",responseObject);
            //NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:nil error:nil];
            NSDictionary *dic = (NSDictionary *)responseObject;
            if (self.rooms.count > 0)
            {
                [self.rooms removeObjectAtIndex:indexPath.row];
                [self.tableView reloadData];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
        {
            NSLog(@"删除出错%@", error);
            
        }];
    

    相关文章

      网友评论

          本文标题:app 服务器的简单搭建 增删数据

          本文链接:https://www.haomeiwen.com/subject/fkzzrktx.html