阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      express+mongoed 接口 API 的设计逻辑

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

          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)

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

      3、api 接口如下

          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;
      
      
      卖前端学习教程

      只需几十元,就能买到培训班的内部教程!开启高薪之路!

      零基础小白阿里P7的教程都有!

      同时长期收购所有培训班的前端教程

      目录
      目录