管理系统需求分析:
注册登录页面:有的管理平台需要注册,有的直接分配权限就能进入。
内页功能??:
增加
查询分类
关键字查询
分页查询
删除
修改
H-ui:架起设计与后端的桥梁,轻量级前端框架
回忆: 开启静态目录,静态资源路径
db/model/foodModel.js
const mongoose=require(‘mongoose‘) var foodSchema = new mongoose.Schema({ name : {type:String,required:true}, price : {type:String,required:true}, desc : {type:String,required:true}, typename:{type:String,required:true}, typeid:{type:Number,required:true}, img:{type:String,required:true} }); // 将schema 对象转化为数据模型 var Food = mongoose.model(‘foods‘, foodSchema);//该数据对象和集合关联(‘集合名‘,schema对象) module.exports=Food // a*10000+b*10000 4
router/foodRouter.js
const express=require(‘express‘) const router= express.Router() const foodModel=require(‘../db/model/foodModel‘)
/** * @api {post} /food/add 添加菜品 * @apiName addfood * @apiGroup Food * * @apiParam {String} name 菜品名称. * @apiParam {String} price 价格. * @apiParam {String} desc 描述. * @apiParam {String} typename 类型. * @apiParam {String} typeid 类型ID. * @apiParam {String} img 图片. * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */ router.post(‘/add‘,(req,res)=>{ // let data={ // name : ‘火山飘雪‘, // price : ‘9999‘, // desc : ‘超好吃‘, // typename:‘凉菜‘, // typeid:1, // img:‘/public/image/img3.jpg‘ // } let {name, price,desc,typename,typeid,img} = req.body // 判断参数是否正确 foodModel.insertMany({name, price,desc,typename,typeid,img}) .then((data)=>{ res.send({err:0,msg:‘添加成功‘}) }) .catch(()=>{ res.send({err:-1,msg:‘添加失败‘}) }) })
/** * @api {post} /food/getInfoByType 分类查询 * @apiName getInfoByType * @apiGroup Food * * @apiParam {Number} typeid 用户名. * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */ router.post(‘/getInfoByType‘,(req,res)=>{ let {typeid}=req.body foodModel.find({typeid}) .then((data)=>{ res.send({err:0,msg:‘查询ok‘,list:data}) }) .catch(()=>{ res.send({err:-1,msg:‘查询失败‘}) }) })
/** * @api {post} /food/getInfoByKw 关键字查询 * @apiName getInfoByKw * @apiGroup Food * * @apiParam {Number} kw 关键字. * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */ router.post(‘/getInfoByKw‘,(req,res)=>{ // $set $gte $or $and $regex regexp 带$都是内置的 let {kw}=req.body let reg=new RegExp(kw) // 创建一个正则表达式 匹配关键字 // foodModel.find({age:{$gte:16}}) // foodModel.find({name:{$regex:reg}}) 名字模糊 console.log(kw) foodModel.find({$or:[{name:{$regex:reg}},{desc:{$regex:reg}}]}) // 名字 以及描述 .then((data)=>{ res.send({err:0,msg:‘查询ok‘,list:data}) }) .catch(()=>{ res.send({err:-1,msg:‘查询失败‘}) }) })
router.post(‘/del‘,(req,res)=>{ let {_id}=req.body //单个删除 多个删除(id传数组) foodModel.remove({_id:id}) .then((data)=>{ res.send({err:0,msg:‘del ok‘}) }) .catch(()=>{ res.send({err:-1,msg:‘删除失败‘}) }) })
router.post(‘/update‘,(req,res)=>{ //修改,编辑 let {name, price,desc,typename,typeid,img,_id} = req.body console.log(req.body) console.log({name, price,desc,typename,typeid,img,_id} ) foodModel.updateOne({_id},{name, price,desc,typename,typeid,img}) .then((data)=>{ res.send({err:0,msg:‘修改 ok‘}) }) .catch(()=>{ res.send({err:-1,msg:‘修改失败‘}) }) })
/** * @api {post} /food/getInfoByPage 分页查询 * @apiName getInfoByPage * @apiGroup Food * * @apiParam {Number} pageSize 每页数据条数. * @apiParam {Number} page 那一页. * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */ router.post(‘/getInfoByPage‘,(req,res)=>{ let pageSize=req.body.pageSize || 2 //设置默认值 let page =req.body.page || 1 foodModel.find().limit(Number(pageSize)).skip(Number((page-1)*pageSize)) .then((data)=>{ res.send({err:0,msg:‘查询ok‘,list:data}) }) .catch(()=>{ res.send({err:-1,msg:‘查询失败‘}) }) })
module.exports=router
server.js
const express=require(‘express‘) const db=require(‘./db/connect‘) const path=require(‘path‘) const app=express() const Mail=require(‘./utils/mail‘) console.log(Mail) const bodypaser=require(‘body-parser‘) app.use(bodypaser.urlencoded({ extended: false })) app.use(bodypaser.json()) app.use(‘/public‘,express.static(path.join(__dirname,‘./hehe‘))) // 路由 const userRouter = require(‘./router/userRouter‘) const fooodRouter= require(‘./router/foodRouter‘) app.use(‘/user‘,userRouter) app.use(‘/food‘,fooodRouter) app.listen(3000,()=>{ console.log(‘server start‘) })