標籤:sts 技術 內容 目錄 nod git pen version json格式
Nodejs構建mock資料並通過rest api風格調用介面訪問資料
如果我們只有json格式的資料檔案,我們想通過訪問url方式調用居然資料
確保電腦安裝node環境
如果你沒有安裝好node環境請移步http://nodejs.cn/
一、安裝json-server
1.建立demo檔案
cd demo
2.安裝json-server
npm install -S json-server
3.項目demo目錄下,建立一個 JSON 檔案data.json和一個package.json檔案
db.json內容如下
{ "posts": [ { "id": 1, "title": "json-server", "author": "typicode" } ], "comments": [ { "id": 1, "body": "some comment", "postId": 1 } ], "profile": { "name": "typicode" }}
package.json內容如下
{ "name": "rest-api-demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "server": "json-server data.json", "test": "..." }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "json-server": "^0.9.4" }}
二、啟動服務
npm run server
三、調用rest 風格api
開啟瀏覽器
注意這個的資料擷取只是get方式請求擷取到的,如需POST、PUT、DELETE等 HTTP 方法的資料體進行解析請參考
https://github.com/ruanyf/jstraining/tree/master/demos#rest-api
nodejs構建mock資料