Giter VIP home page Giter VIP logo

react-mock-demo's Introduction

前端mock数据实现伪后台

两种实现方式,具体工程demo打开具体文件夹查看。

  1. mockjs
  2. roadhog
    注:
  3. roadhog也可以利用mockjs造数据,只是这时的mockjs不承担拦截ajax请求的任务
  4. demo做得比较粗糙,输出信息都在控制台,建议自己按照文档思路理解即可

mockjs实现

这种方式只支持ajax和axios,不支持fetch
Mock.js官方API文档
Getting Started

数据获取方式

ajax

使用jquery的ajax发送请求

const dataA = $.ajax({
  url: '/index',
  dataType: 'json'
})
console.log("dataA" ,dataA)

mock收到的Options与ajax得到的返回值:
ajax请求options和返回值 得到的返回值如下:
ajax请求返回值

fetch

fetch用法 fetch API详解 mozila
fetch存在各浏览器兼容性问题,使用时需要添加相应polyfill

问题处理

fetch得到404
mockjs github issues不支持fetch
说是mockjs不支持fetch,只支持ajax...

axios

axios中文文档附API
axios亲测可用,这也是现在项目中用到的获取方式

axios.post('/axios', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
  console.log("axios收到的response", response);
})
.catch(function (error) {
  console.log("axios error", error);
});

mock得到的options和axios收到的response如图所示:
mock得到的options和axios收到的response

roadhog实现mock

偷了个懒,直接用dva-cli创建的工程
主要是想确认下roadhog的mock功能和mockjs有没有关系
事实证明mockjs并不是必须的,项目里的mockjs只起到了造数据的作用,拦截ajax请求是roadhog自身提供的(内部或许有使用到类似技术)

参考文档

roadhog-github

roadhog mock实现步骤

.roadhogrc.mock.js中export 一个mock对象即可,官方示例如下。

export default {
  // 支持值为 Object 和 Array
  'GET /api/users': { users: [1,2] },

  // GET POST 可省略
  '/api/users/1': { id: 1 },

  // 支持自定义函数,API 参考 express@4
  'POST /api/users/create': (req, res) => { res.end('OK'); },

  // Forward 到另一个服务器
  'GET /assets/*': 'https://assets.online/',

  // Forward 到另一个服务器,并指定子路径
  // 请求 /someDir/0.0.50/index.css 会被代理到 https://g.alicdn.com/tb-page/taobao-home, 实际返回 https://g.alicdn.com/tb-page/taobao-home/0.0.50/index.css
  'GET /someDir/(.*)': 'https://g.alicdn.com/tb-page/taobao-home',
};

注意自定义函数应用相当广,可以返回特定状态码等信息,简单示例如下:

  [`POST ${url}`]: (req, res) => {
    const { query } = req
    console.log('query', query)
    res.status(300).json({
      data: data[query],
      total: data.length,
    })
  }

react-mock-demo's People

Contributors

vonxq avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.