express+mongoed 接口 API 的设计逻辑
   1 分钟阅读

1、定义表结构(/schemas/user.js)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    //表结构
    var userSchema = new Schema({
        username:String,
        password:String,
        isAdmin:{
            type :Boolean,
            default:false
        }
    });
    module.exports = userSchema;

2、定义表模型(/models/User.js)

1
2
3
4
    var mongoose = require('mongoose');
    var userSchema = require('../schemas/user');//先引用表结构
    var User = mongoose.model('User', userSchema);//指定表模型
    module.exports = User;

3、api 接口如下

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
    var express = require("express");
    var router = express.Router();
    var User = require("../models/User");
    var responseData = null;
    router.use(function (req, res, next) {
        responseData= {
            code:0,
            message:""
        }
        next();
    })
    // api/user/register?username=zhuanbang&password=12345678&repassword=12345678
    router.post('/user/register', function (req, res, next) {
        var username=req.body.username;
        var password=req.body.password;
        var repassword=req.body.repassword;
        console.log('req.body', req.body)
        if(!username){
            responseData.code=1;
            responseData.message="username is null"
            returnres.json(responseData)
        }
        if(!password){
            responseData.code=2;
            responseData.message="password is null"
            returnres.json(responseData)
        }
        if(password!==repassword){
            responseData.code=3;
            responseData.message="password is diff"
            returnres.json(responseData)
        }
        User.findOne({"username":username}).then(function(data){
            if(!data){
                //插入
                var user=newUser({
                    "username":username,
                    "password":password
                });
                user.save().then(function(userInfo){
                    returnuserInfo;
                }).then(function(userInfo){
                    if(userInfo){
                        responseData.code=0;
                        responseData.message="success"
                        req.cookies.set('userInfo',JSON.stringify({
                        "username":username,
                        "_id":userInfo._id
                        }));
                        returnres.json(responseData)
                    }
                })
            }else{
                responseData.code=4;
                responseData.message="is in db"
                returnres.json(responseData)
            }
        })
        // res.json({"data":req});
    })
    router.post("/user/login",function(req,res,next){
        var username=req.body.username;
        var password=req.body.password;
        if(!username){
            responseData.code=1;
            responseData.message="username is null"
            returnres.json(responseData)
        }
        if(!password){
            responseData.code=2;
            responseData.message="password is null"
            returnres.json(responseData)
        }
        //是否在数据库中
        User.findOne({
            "username":username,
            "password":password,
        }).then(function(data){
            if(!data){
                responseData.code=4;
                responseData.message="Error !!!!"
                returnres.json(responseData)
            }else{
                responseData.message="success"
                responseData.data={
                    _id:data._id,
                    username:data.username,
                }
                req.cookies.set('userInfo',JSON.stringify(responseData.data));
                returnres.json(responseData)
            }
        })
    })
    router.get("/user/logout",function(req,res,next){
        req.cookies.set('userInfo',");
        responseData.message="success"
        returnres.json(responseData)
    })
    module.exports = router;