nodeJS开发一套完整的项目(7、食品分类模块)

本章节我们谈谈食品分类模块怎么写。同样,我们还是从表的创建、控制器、路由等这几块入手。

nodeJS开发一套完整的项目(7、食品分类模块)_第1张图片
数据库截图

创建数据

由于我们没有后台的管理平台,所以这里面的数据我们需要手动创建,主要目的就是方便调试。我们在initData文件夹下创建entry.js,然后给它赋值。

export default [
{
id: 15,
is_in_serving: true,
description: "附近美食一网打尽",
title: "美食",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu7f8e%5Cu98df%22%2C%22complex_category_ids%22%3A%5B207%2C220%2C233%2C260%5D%2C%22is_show_all_category%22%3Afalse%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E7%BE%8E%E9%A3%9F&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/b/7e/d1890cf73ae6f2adb97caa39de7fcjpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 20,
is_in_serving: true,
description: "苦了累了,来点甜的",
title: "甜品饮品",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu751c%5Cu54c1%5Cu996e%5Cu54c1%22%2C%22complex_category_ids%22%3A%5B240%2C241%2C242%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A239%2C%22name%22%3A%22%5Cu751c%5Cu54c1%5Cu996e%5Cu54c1%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E7%94%9C%E5%93%81%E9%A5%AE%E5%93%81&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/2/35/696aa5cf9820adada9b11a3d14bf5jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 10,
is_in_serving: true,
description: "足不出户,便利回家",
title: "商超便利",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu5546%5Cu8d85%5Cu4fbf%5Cu5229%22%2C%22complex_category_ids%22%3A%5B254%2C255%2C256%2C257%2C258%2C271%2C272%2C273%2C274%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A252%2C%22name%22%3A%22%5Cu5546%5Cu5e97%5Cu8d85%5Cu5e02%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E5%95%86%E8%B6%85%E4%BE%BF%E5%88%A9&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/0/da/f42235e6929a5cb0e7013115ce78djpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 1,
is_in_serving: true,
description: "0元早餐0起送,每天都有新花样。",
title: "预订早餐",
link: "eleme://web?url=https%3A%2F%2Fzaocan.ele.me&target_name=%E9%A2%84%E8%AE%A2%E6%97%A9%E9%A4%90&animation_type=1&is_need_mark=&banner_type=",
image_url: "/d/49/7757ff22e8ab28e7dfa5f7e2c2692jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 8,
is_in_serving: true,
description: "一天变女神",
title: "果蔬生鲜",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu679c%5Cu852c%5Cu751f%5Cu9c9c%22%2C%22complex_category_ids%22%3A%5B245%2C246%2C247%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A244%2C%22name%22%3A%22%5Cu679c%5Cu852c%5Cu751f%5Cu9c9c%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E6%9E%9C%E8%94%AC%E7%94%9F%E9%B2%9C&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/4/34/ea0d51c9608310cf41faa5de6b8efjpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 403297,
is_in_serving: true,
description: "大胆尝鲜,遇见惊喜",
title: "新店特惠",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu65b0%5Cu5e97%5Cu7279%5Cu60e0%22%2C%22complex_category_ids%22%3A%5B207%2C220%2C233%2C239%2C244%2C248%2C252%2C260%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22support_ids%22%3A%5B-1%5D%2C%22activities%22%3A%5B%5D%7D&target_name=%E6%96%B0%E5%BA%97%E7%89%B9%E6%83%A0&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/a/fa/d41b04d520d445dc5de42dae9a384jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 92,
is_in_serving: true,
description: "准时必达,超时赔付",
title: "准时达",
link: "eleme://restaurants?filter_key=%7B%22support_ids%22%3A%5B9%5D%2C%22activities%22%3A%5B%7B%22id%22%3A9%2C%22name%22%3A%22%5Cu51c6%5Cu65f6%5Cu8fbe%22%2C%22icon_name%22%3A%22%5Cu51c6%22%2C%22icon_color%22%3A%22E8842D%22%2C%22is_need_filling%22%3A0%2C%22is_multi_choice%22%3A1%2C%22filter_value%22%3A9%2C%22filter_key%22%3A%22support_ids%22%2C%22description%22%3A%22%5Cu51c6%5Cu65f6%5Cu8fbe%22%7D%5D%7D&target_name=%E5%87%86%E6%97%B6%E8%BE%BE&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/3/84/8e031bf7b3c036b4ec19edff16e46jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 225,
is_in_serving: true,
description: "有菜有肉,营养均衡",
title: "简餐",
link: "eleme://restaurants?filter_key=%7B%22activity_types%22%3A%5B3%5D%2C%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu7b80%5Cu9910%22%2C%22complex_category_ids%22%3A%5B209%2C212%2C215%2C265%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%7B%22id%22%3A3%2C%22name%22%3A%22%5Cu4e0b%5Cu5355%5Cu7acb%5Cu51cf%22%2C%22icon_name%22%3A%22%5Cu51cf%22%2C%22icon_color%22%3A%22f07373%22%2C%22is_need_filling%22%3A1%2C%22is_multi_choice%22%3A0%2C%22filter_value%22%3A3%2C%22filter_key%22%3A%22activity_types%22%7D%5D%7D&target_name=%E7%AE%80%E9%A4%90&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/d/38/7bddb07503aea4b711236348e2632jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 65,
is_in_serving: true,
description: "",
title: "土豪推荐",
link: "eleme://restaurants?filter_key=%7B%22activities%22%3A%5B%7B%22filter_key%22%3A%22tags%22%2C%22filter_value%22%3A0%7D%5D%7D&target_name=%E5%9C%9F%E8%B1%AA%E6%8E%A8%E8%8D%90&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/e/7e/02b72b5e63c127d5bfae57b8e4ab1jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 236,
is_in_serving: true,
description: "大口大口把你吃掉",
title: "汉堡薯条",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu6c49%5Cu5821%22%2C%22complex_category_ids%22%3A%5B212%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E6%B1%89%E5%A0%A1%E8%96%AF%E6%9D%A1&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/b/7f/432619fb21a40b05cd25d11eca02djpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 289,
is_in_serving: true,
description: "老字号,好味道",
title: "包子粥店",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu5305%5Cu5b50%5Cu7ca5%5Cu5e97%22%2C%22complex_category_ids%22%3A%5B215%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E5%8C%85%E5%AD%90%E7%B2%A5%E5%BA%97&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/2/17/244241b514affc0f12f4168cf6628jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 9,
is_in_serving: true,
description: "内心小公举,一直被宠爱",
title: "鲜花蛋糕",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu9c9c%5Cu82b1%5Cu86cb%5Cu7cd5%22%2C%22complex_category_ids%22%3A%5B249%2C250%2C251%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A248%2C%22name%22%3A%22%5Cu9c9c%5Cu82b1%5Cu86cb%5Cu7cd5%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E9%B2%9C%E8%8A%B1%E8%9B%8B%E7%B3%95&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/8/83/171fd98b85dee3b3f4243b7459b48jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 286,
is_in_serving: true,
description: "",
title: "麻辣烫",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu9ebb%5Cu8fa3%5Cu70eb%22%2C%22complex_category_ids%22%3A%5B214%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A207%2C%22name%22%3A%22%5Cu5feb%5Cu9910%5Cu4fbf%5Cu5f53%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E9%BA%BB%E8%BE%A3%E7%83%AB&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/3/c7/a9ef469a12e7a596b559145b87f09jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 288,
is_in_serving: true,
description: "无辣不欢",
title: "川湘菜",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu5ddd%5Cu6e58%5Cu83dc%22%2C%22complex_category_ids%22%3A%5B221%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A220%2C%22name%22%3A%22%5Cu7279%5Cu8272%5Cu83dc%5Cu7cfb%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E5%B7%9D%E6%B9%98%E8%8F%9C&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/9/7c/9700836a33e05c2410bda8da59117jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 287,
is_in_serving: true,
description: "西餐始祖,欧洲的味道",
title: "披萨意面",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu62ab%5Cu8428%5Cu610f%5Cu9762%22%2C%22complex_category_ids%22%3A%5B211%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A260%2C%22name%22%3A%22%5Cu5f02%5Cu56fd%5Cu6599%5Cu7406%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E6%8A%AB%E8%90%A8%E6%84%8F%E9%9D%A2&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/7/b6/235761e50d391445f021922b71789jpeg.jpeg",
icon_url: "",
title_color: ""
},
{
id: 285,
is_in_serving: true,
description: "寿司定食,泡菜烤肉",
title: "日韩料理",
link: "eleme://restaurants?filter_key=%7B%22category_schema%22%3A%7B%22category_name%22%3A%22%5Cu65e5%5Cu97e9%5Cu6599%5Cu7406%22%2C%22complex_category_ids%22%3A%5B229%5D%2C%22is_show_all_category%22%3Atrue%7D%2C%22restaurant_category_id%22%3A%7B%22id%22%3A260%2C%22name%22%3A%22%5Cu5f02%5Cu56fd%5Cu6599%5Cu7406%22%2C%22sub_categories%22%3A%5B%5D%2C%22image_url%22%3A%22%22%7D%2C%22activities%22%3A%5B%5D%7D&target_name=%E6%97%A5%E9%9F%A9%E6%96%99%E7%90%86&animation_type=1&is_need_mark=0&banner_type=",
image_url: "/6/1a/1e0f448be0624c62db416e864d2afjpeg.jpeg",
icon_url: "",
title_color: ""
}
]

创建食品分类模型

这里我们创建食品分类表结构,字段与上面相同。我们在web->models->v2文件夹下创建entry.js,代码如下:

'use strict';

import mongoose from 'mongoose';
import entryData from '../../../initData/entry';

//创建表结构的方法我就不多说了,大家看看之前写的就知道了。
const Schema = mongoose.Schema;

const entrySchema = new Schema({
    id: Number,
    is_in_serving: Boolean,
    description: String,
    title: String,
    link: String,
    image_url: String,
    icon_url: String,
    title_color: String
});

const Entry = mongoose.model('Entry',entrySchema);

Entry.findOne((err, data) => {
    if(!data) {
        for(let i = 0; i < entryData.length; i++ ) {
            Entry.create(entryData[i]);
        }
    }
});

export default Entry;

编写控制器

控制器里其实很简单,就是显示数据,具体代码如下:

'use strict';
import EntryModel from '../../models/v2/entry';

class Entry {
    constructor() {}

    async getEntry(req, res, next) {
        try{
            const entries = await EntryModel.find({},'-_id'); //这里说明一下,find函数中第一个参数是字段名,用{}表示全部字段,第二个参数说明不想显示的字段,注意:不想显示的字段前面一定要加“-”,否则会失败
            res.send(entries);
        } catch(err) {
            console.log('获取数据失败');
            res.send({
                status: 0,
                type: 'ERROR_DATA',
                message: '获取数据失败'
            });
            return;
        }
    }
}

export default new Entry();

编写路由

当前面的工作完成后,我们就可以编写路由。在routes文件夹下,创建v2.js,如果之前创建过了可以直接在里面写,路由内容如下:

import express from 'express';
import Entry from '../web/controller/v2/entry';
const router = express.Router();

router.get('/index_entry', Entry.getEntry);
export default router

当路由写好后,我们就在控制台启动项目,启动命令:npm run dev。然后在浏览器中输入:http://127.0.0.1:3000/v2/index_entry,就能看到我们想要的效果了。截图如下:

nodeJS开发一套完整的项目(7、食品分类模块)_第2张图片
效果图

相关章节
nodeJS开发一套完整的项目(1、基础配置)
nodeJS开发一套完整的项目(2、相关模块介绍)
nodeJS开发一套完整的项目(3、数据库链接和项目启动)
nodeJS开发一套完整的项目(4、编写底层功能模块)
nodeJS开发一套完整的项目(5、开发用户模块)
nodeJS开发一套完整的项目(6、省市县模块)
为了更好的服务大家,请加入我们的技术交流群:( 511387930),同时您也可以扫描下方的二维码关注我们的公众号,每天我们都会分享经验,谢谢大家。
nodeJS开发一套完整的项目(7、食品分类模块)_第3张图片

你可能感兴趣的:(nodeJS开发一套完整的项目(7、食品分类模块))