在開發過程當中,先後端不管是否分離,接口多半是滯後於頁面開發的。因此創建一個REST風格的API接口,給前端頁面提供虛擬的數據,是很是有必要的。前端
對比過多種mock工具後,我最終選擇了使用 json server
做爲工具,由於它足夠簡單,寫少許數據,便可使用。
也由於它足夠強大,支持CORS和JSONP跨域請求,支持GET, POST, PUT, PATCH 和 DELETE 方法,更提供了一系列的查詢方法,如limit,order等。下面我將詳細介紹 json server
的使用。node
首先你的電腦中須要安裝nodejs,建議使用最新版本。而後全局安裝json server.jquery
npm install json-server -g
使用linux和macOS的電腦須要加上sudolinux
sudo npm install json-server -g
安裝完成後能夠用 json-server -h
命令檢查是否安裝成功,成功後會出現git
json-server [options] <source> 選項: --config, -c Path to config file [默認值: "json-server.json"] --port, -p Set port [默認值: 3000] --host, -H Set host [默認值: "0.0.0.0"] --watch, -w Watch file(s) [布爾] --routes, -r Path to routes file --static, -s Set static files directory --read-only, --ro Allow only GET requests [布爾] --no-cors, --nc Disable Cross-Origin Resource Sharing [布爾] --no-gzip, --ng Disable GZIP Content-Encoding [布爾] --snapshots, -S Set snapshots directory [默認值: "."] --delay, -d Add delay to responses (ms) --id, -i Set database id property (e.g. _id) [默認值: "id"] --quiet, -q Suppress log messages from output [布爾] --help, -h 顯示幫助信息 [布爾] --version, -v 顯示版本號 [布爾] 示例: json-server db.json json-server file.js json-server http://example.com/db.json https://github.com/typicode/json-server
安裝完成後,能夠在任一目錄下創建一個 xxx.json
文件,例如在 mock/
文件夾下,創建一個 db.json
文件,並寫入如下內容,並在 mock/
文件夾下執行 json-server db.json -p 3003
。
github
{ "news":[ { "id": 1, "title": "曹縣宣佈昨日晚間登日成功", "date": "2016-08-12", "likes": 55, "views": 100086 }, { "id": 2, "title": "長江流域首次發現海豚", "date": "2016-08-12", "likes": 505, "views": 9800 } ], "comments":[ { "id": 1, "news_id": 1, "data": [ { "id": 1, "content": "支持黨中央決定" }, { "id": 2, "content": "抄寫黨章勢在必行!" } ] } ] }
爲了方便,再建立一個 package.json
文件,寫入ajax
{ "scripts": { "mock": "json-server db.json --port 3003" } }
而後使用到 /mock
目錄下執行 npm run mock
命令,若是成功會出現npm
> @ mock /你的電腦中mock文件夾所在目錄的路徑/mock > json-server db.json -p 3003 \{^_^}/ hi! Loading db.json Done Resources http://localhost:3003/news http://localhost:3003/comments Home http://localhost:3003
若是不成功請檢查 db.json
文件的格式是否正確。json
這個時候訪問 http://localhost:3003/db
能夠查看 db.json
文件中所定義的所有數據。
使用瀏覽器地址欄,jQuery.get
或 fecth({method: "get"})
訪問 http://localhost:3003/news
,則能夠看到 news
對象下的數據,以Array格式返回:後端
[ { "id": 1, "title": "曹縣宣佈昨日晚間登日成功", "date": "2016-08-12", "likes": 55, "views": 100086 }, { "id": 2, "title": "長江流域首次發現海豚", "date": "2016-08-12", "likes": 505, "views": 9800 } ]
以jquery的 $.ajax
方法舉例,如下代碼會實時的向 db.json
中的 news
對象push一條新的數據再次用 get
方式訪問 http://localhost:3003/news
, 就能夠看到它了
$.ajax({ type: 'post', url: 'http://localhost:3003/news', data: { "id": 3, "title": "我是新加入的新聞", "date": "2016-08-12", "likes": 0, "views": 0 } } )
一樣以jquery的 $.ajax
方法舉例,如下代碼會實時的對 db.json
中的 news
對象中 id=1
數據進行修改
$.ajax({ type: 'put', url: 'http://localhost:3003/news/1', data: { "title": "曹縣宣佈昨日晚間登日失敗", "date": "2016-08-12", "likes": 55, "views": 100086 } } ) // 結果 [ { "id": 1, "title": "曹縣宣佈昨日晚間登日失敗", "date": "2016-08-12", "likes": 55, "views": 100086 } ]
PATCH 和 DELETE 使用方式同上,就不作演示了。
json-server源碼 : json-server
mockjs源碼 : mockjs
demo : 示例代碼