1 Star 0 Fork 0

mmyhs / generator-mock-koa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

generator-mock-koa

使用koa结合mock,主要提供给前端开发人员的模拟数据测试,必要的时候,也可以用于代理转发、和基于Koa开发新的系统

Installation

First, install Yeoman and generator-mock-koa using npm (we assume you have pre-installed node.js).

npm install -g yo
npm install -g generator-mock-koa

Then generate your new project:

yo mock-koa

1.1.2

实现了基础的功能,能够基于简单配置实现数据的mock 升级特性:构建完成后,会自动安装相关依赖,使用者只需要到根目录下执行 npm run mock-serve 就能开启mock服务了

Use

项目构建完成后,打开src/api,在这个页面下编写mock的接口,在该目录下的文件会自动处理,要求符合格式

案列:使用模板解析语法开发

import Mock from 'mockjs';

const data = Mock.mock({
  total: 100,
  'rows|10': [{
    id: '@guid',
    name: '@cname',
    'age|20-30': 23,
    'job|1': ['前端工程师', '后端工程师', 'UI工程师', '需求工程师']
  }]
});

export default {
  prefix: "/pattern",
  methods: {
    "GET /a": data,
    "GET /list": () => {
      return {
        code: 200,
        message: "success",
        data
      }
    },
    // 这里的 ctx ==> router.get(ctx);
    "/list2": (ctx) => {
      return {
        code: 200,
        message: "success",
        data: ctx.request.query
      }
    },
    "POST /form": () => {
      return {
        code: 200,
        message: "success",
        data: Mock.mock({ id: '@guid' })
      }
    }
  }
};

案列,使用原生Koa的开发模式

import Router from 'koa-router';
import Mock from 'mockjs';

/**
 * 案列,使用原生Koa的开发模式
 */

const router = new Router({
  prefix: '/user',
});

/**
 * 获取菜单列表
 */
router.get('/list', (ctx) => {
  const data = Mock.mock({
    total: 100,
    'rows|10': [{
      id: '@guid',
      name: '@cname',
      'age|20-30': 23,
      'job|1': ['前端工程师', '后端工程师', 'UI工程师', '需求工程师']
    }]
  });
  return {
    code: 200,
    message: "success",
    data
  }
});

export default router;

Getting To Know Yeoman

  • Yeoman has a heart of gold.
  • Yeoman is a person with feelings and opinions, but is very easy to work with.
  • Yeoman can be too opinionated at times but is easily convinced not to be.
  • Feel free to learn more about Yeoman.

License

MIT © mmyhs

The MIT License (MIT) Copyright (c) 2022 mmyhs <luo_ming_ming@163.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用koa结合mock,主要提供给前端开发人员的模拟数据测试,必要的时候,也可以用于代理转发 展开 收起
JavaScript
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mmyhs/generator-mock-koa.git
git@gitee.com:mmyhs/generator-mock-koa.git
mmyhs
generator-mock-koa
generator-mock-koa
develop

搜索帮助