業餘時間基於 Node.js 搭建了 Mock Server 一枚,自娛自樂的產物。功能較簡易,很是很是很是小白級,但可知足絕大多需求。html
cd meow-mock npm install npm run start
默認端口號 8888,可在 /bin/www 文件中修改:node
var port = normalizePort(process.env.PORT || '8888');
未安裝 Node.js 的請先移駕此處下載並安裝。git
未安裝 Npm 的請先執行:github
curl -L https://npmjs.com/install.sh | sh
在 /data 文件夾內建立 .js 接口文件,例如 example.js。打開 route.js 添加以下代碼:npm
var example = require('./data/example'); var requestGatherLst = [ example ];
可理解爲在該服務中註冊了剛剛建立的接口文件。咱們能夠建立多個接口文件 module1.js、module2.jd、module3.js... 瀏覽器
假定 example.js 代碼以下:bash
var type = require('../type'); module.exports = { example1: { url: '/example1/_data', type: 'GET', data: function () { return {} } }, example2: { url: '/example2/_data', type: 'POST', data: function () { return {} } } }
顯然,須要定義每一接口的 url 及 type ,返回數據的講究在於 data 回調函數的返回值。curl
舉個板栗:函數
全部數據都可動態生成
example1: { url: '/example1/_data', type: 'GET', data: function () { return { message: '請求成功', error: 0, data: { id: type.id(), // 返回 id number: type.number({ // 返回數值 min: 288, max: 999 }), bool: type.bool(), // 返回布爾值 string1: type.string([ // 返回字符串 '文案一', '文案二', '文案三' ]), string2: type.string({ // 返回字符串 minL: 5, maxL: 16 }), image1: type.image([ // 返回圖片連接 'http://oij8a9ql4.bkt.clouddn.com/default-fe.jpg', 'http://osm0bpix4.bkt.clouddn.com/thumb.jpg' ]), image2: type.image({ // 返回圖片連接 type: '-thumb' }), list1: type.list({ // 返回列表 length: 5, data: function () { return type.number() } }), list2: type.list({ // 返回列表 length: 22, index: { name: 'idx', format: '0\d' }, data: function () { return { pro1: type.number(), pro2: type.string() } } }) } } } }
此處有坑!假定一列表長度爲 n,列表項含字段 id。生成每一列表項的時間差很是很是很是小,那麼:
id 怎麼能夠重複...想辦法去重嘍~
module.exports = { timestamps: {}, id: function () { var _this = this; var curtime = (new Date()).valueOf(); var recursion = function (key) { if (_this.timestamps[key]) { var tmp = recursion(key + 1); } else { _this.timestamps[key] = 1; return key; } return tmp; }; return recursion(curtime); } }
打開 http://localhost:8888/example... 查看返回數據以下:
因爲數據是 動態生成 的,你所看到的結果可能與個人不一樣嗷~
但在重啓服務以前,同一 URL 下,刷新瀏覽器是不會影響返回數據的,除非改變參數值或添加新的參數。
打開 http://localhost:8888/example... 體會下!
再打開 http://localhost:8888/example... 體會下!
渲染列表數據時,每每須要渲染其序號,例如排行榜:
index 配置項即是爲上述需求而生:
index: { name: 'idx', format: '\d', type: 'int' }
關於 format
具體效果形如:
推薦:
list: type.list({ length: 5, data: function() { return type.number() } })
返回列表值不一樣:
不推薦:
list: type.list({ length: 5, data: type.number() })
返回列表值相同:
多數狀況咱們不喜歡這樣的結果~
首先說明列表數據請求接口對象寫法:
example2: { url: '/example2/_data', type: 'GET', list_name: 'items', data: function () { return { message: '請求成功', error: 0, items: type.list({ length: 36, index: { name: 'idx', format: '0\d' }, data: function () { return { id: type.id(), number: type.number(), string: type.string(), image: type.image() } } }) } } }
list_name 配置項決定了返回數據中究竟哪一個字段是待分段的 list,其默認值爲 data。
假如數據總長 36,每一分頁數據長度 count=10,那麼:
page=1 時,返回第 1 ~ 10 條數據;
page=2 時,返回第 11 ~ 20 條數據;
page=3 時,返回第 21 ~ 30 條數據;
page=4 時,返回第 31 ~ 36 條數據;
page > 4 時,返回空列表。
打開 http://localhost:8888/example... 體會下!
截取列表中第 start_num + 1 至 start_num + count 條數據。假如 ?start_num=6&count=5,那麼返回第 7 ~ 11 條數據。
打開 http://localhost:8888/example... 體會下!
備註:
做者:呆戀小喵
個人後花園:https://sunmengyuan.github.io...
個人 github:https://github.com/sunmengyuan