三步使用 JSON Server 模擬 API

jopen 9年前發布 | 89K 次閱讀 JSON JSON開發包

有了設計圖之后,后端的 API 往往也才剛剛開始做,在這個時候,能有一個原型工具模擬一份 API 會讓一切過的開心很多。

安裝

第一步當然是安裝啦

Homebrew

Homebrew 是 Mac 上的第三方庫管理工具,我們使用 Homebrew 來安裝 Node.js

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Node.js

brew install node

json-server

npm install -g json-server

創建 db.json

找個你喜歡的目錄,創建 db.json,里面包含你需要的數據模型

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

隨后,利用 json-server --watch db.json 來啟動 Server

測試

啟動起來之后,json-server 會根據你的 db.json 自動建立 RESTful API,以上面的 db.json 的 post 為例

GET    /posts  
GET    /posts/1  
POST   /posts  
PUT    /posts/1  
PATCH  /posts/1  
DELETE /posts/1

更深入的使用方式,可以參考 json-server 的官方文檔。

來自:http://tips.producter.io/san-bu-shi-yong-json-server-mo-ni-api/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!