simple-mock-api

項目中有一個Activiy需要輪詢多個接口,實時刷新數據。這裏很多業務邏輯都需要依賴當前的狀態,所以簡單寫了一個服務端,用於生成一些mock data,並且支持數據實時刷新。

項目地址:simple-mock-api

掛到騰訊雲上就可以公網訪問了。


simple-mock-api

Use json-server make this simple mock api server.

Usage

  1. checkout this project,and cd
  2. type npm install in terminal to install dependences
  3. run node app.js
  4. get result via your configed cgi,like http://localhost:3000/get_match_players

Config

You can config

cgi,
default json file,
custom handle the request, modify json response

Example

Create a get_match_detail.json file in data folder.

{
  "state": 1,
  "title":"NBA",
  "time":"2017-01-01",
  "homescore": "101",
  "guestscore": "115"
}

In config.js config you mock api

var config = [
{
    "cgi":"/get_match_detail",
    "filepath":"data/get_match_detail.json",
    "need_cache":true,
    "hookfunc":func_get_match_detail
},
{
    "cgi":"/get_match_players",
    "filepath":"data/get_match_players.json",
    "need_cache":false,
    "hookfunc":func_get_match_players
},

];

You can custom handle the request by define your custom “hookfunc”, like this:

var func_get_match_detail = function(defaultObj,req) {
    //increase homescore every time
    var homescore = parseInt(defaultObj.homescore)+1+"";
    defaultObj.homescore = homescore;
}

The param defaultObj means the parsed json object you defined in the key “filepath”.

If the key “need_cache” seted true, than your hookfunc can change the defaultObj in the cache forever.

Start JSON Server

$ node app.js

Now if you go to http://localhost:3000/get_match_detail, you’ll get

{
  "state": 1,
  "title":"NBA",
  "time":"2017-01-01",
  "homescore": "102",
  "guestscore": "115"
}

and the homescore’s value changed every time when you go to the same link.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章