Router路由的实现

在router文件中,所有前台访问的接口在此实现。

Rooms:

var express = require('express');
var router = express.Router(); //引用router模块
var Rooms = require('../models/Rooms');
var multiparty = require('multiparty'); //实现文件上传模块
var util = require('util'); //提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足
var fs = require('fs'); //文件系统模块(

//获取所有房间信息
router.get('/getallrooms', function (req, res, next) {
    var count = parseInt(req.query.count);
    Rooms.find({}, function (err, rooms) {
        if (err) {
            return next(err);
        }
        //console.log(rooms);
        res.send({rooms: rooms});
    }).limit(count); //只查询4条数据
});
//按分类查找房间列表
router.get('/getcategoryrooms', function (req, res, next) {
    var bigCategory = req.query.bigCategory;
    var smallCategory = req.query.smallCategory;
    var count = parseInt(req.query.count);
    Rooms.find({BigCategory: bigCategory, SmallCategory: smallCategory}, function (err, rooms) {
        if (err) {
            return next(err);
        }
            res.send({rooms: rooms});
    }).limit(count);
});
//查找我的房间列表
router.get('/getmyrooms', function (req, res, next) {
    var userId = req.query.userId;
    var count = parseInt(req.query.count);
    Rooms.find({UserId: userId}, function (err, rooms) {
        if (err) {
            return next(err);
        }
        res.send({rooms: rooms});
    }).limit(count);
});
//添加房间照片
router.post('/addroomphoto/:id', function (req, res) {
    //生成multiparty对象,并配置上传目标路径
    var id = req.params.id;
    var form = new multiparty.Form({uploadDir: './App/resource/photos/'});
    //上传完成后处理
    form.parse(req, function (err, fields, files) {
        var filesTmp = JSON.stringify(files, null, 2);
        //上传完成后处理
        if (err) {
            console.log('parse error: ' + err);
        } else {
            console.log('parse files: ' + filesTmp);
            var inputFile = files.file[0];
            var uploadedPath = inputFile.path;
            var dstPath = './App/resource/photos/' + inputFile.originalFilename;
            //重命名为真实文件名
            fs.rename(uploadedPath, dstPath, function (err) {
                if (err) {
                    console.log('rename error: ' + err);
                } else {
                    console.log('rename ok');
                }
                //修改房间的突破路径
                Rooms.findById(id, function (err, room) {
                    if (err) {
                        return err;
                    }
                    room.Img = inputFile.originalFilename;
                    room.save(function (err) {
                        if (err) {
                            return err;
                        }
                        res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'});
                        res.write('success');
                        res.end();
                    });
                })
            });
        }
    });


});
//创建房间
router.post('/addroom', function (req, res) {
    var room = new Rooms();
    room.Name = req.body.Name;
    room.BigCategory = req.body.BigCategory;
    room.SmallCategory = req.body.SmallCategory;
    room.Img = "base.jpg";
    room.CreateTime = Date.now();
    room.UserId = req.body.UserId;
    room.save(function (err, room) {
        if (err) {
            console.log('save failed');
        }
        console.log('save success');
        res.send({state: 'success', room: room});
    });
});
//检查房间所有者
router.post('/checkRoomByUserId', function (req, res) {

    var roomId = req.body.RoomId;
    var userId = req.body.UserId;

    Rooms.find({_id: roomId, UserId: userId}, function (err, room) {
        if (err) {
            console.log('find failed');
        }
        if (room && room.length > 0) {
            res.send({state: 'success'});
        }
        else {
            res.send({state: 'error'});
        }
    });
});
module.exports = router;

Users

var express = require('express');
var router = express.Router();
var Users = require('../models/Users');
var user = new Users();
/* GET users listing. */
//检查登录信息
router.post('/login', function(req, res, next) {
  var userName=req.body.UserName;
  var passWord=req.body.PassWord;
  Users.find({UserName:userName,PassWord:passWord}, function (err, user) {
      if(err){
       return err;
      }
    if(user&&user.length>0){
      res.send({state:'success',user:user});
    }
    else{
      res.send({state:'error'});
    }

  })
});
//添加用户
router.post('/adduser', function (req, res) {
  user.Name = req.body.Name;
  user.UserName = req.body.UserName;
  user.PhoneNumber = req.body.PhoneNumber;
  user.IdCard = req.body.IdCard;
  user.Identity = req.body.Identity;
  user.PassWord = req.body.PassWord;
  user.CreateTime = Date.now();
  user.save(function (err,result) {
    if (err) {
      console.log('save failed');
      res.send({state:'error',user:result});
    }
    res.send({state:'success',user:result});
  });
});
module.exports = router;

 

转载于:https://www.cnblogs.com/y8932809/p/6185983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值