json檔案可以理解為資料庫
一、json-server快速搭建RESTAPI
安裝:
sudo cnpm install -g json-server
啟動(使用):
json-server指向json檔案所在位置
data.json檔案:
{ "roles": [ { "id": 0, "name": "SuperUser" }, { "id": 1, "name": "User" }, { "id": 2, "name": "Project Manager" } ], "users": [ { "password": "wp123456", "name": "王芃", "avatar": "avatars:svg-1", "email": "wpcfan@163.com", "id": "37489e0c-df34-c261-71c4-ce75357e3035", "projectIds": [ "Sk2HaTagb", "Hya1moGb-" ], "roleIds": [ 0, 1, 2 ] }, { "password": "Ls123456", "name": "李四", "avatar": "avatars:svg-2", "email": "lisi@163.com", "id": "BkkDvwee-", "projectIds": [ "Sk2HaTagb" ], "roleIds": [ 1 ] } ], "projects": [ { "id": "Sk2HaTagb", "name": "問題跟蹤系統", "desc": "用於 Bug 的內部跟蹤和管理", "coverImg": "/assets/img/covers/1.jpg", "members": [ "37489e0c-df34-c261-71c4-ce75357e3035", "BkkDvwee-" ] }, { "name": "某某公司 ERP 系統", "desc": "為某某公司開發的定製化 ERP 系統", "coverImg": "/assets/img/covers/20.jpg", "members": [ "37489e0c-df34-c261-71c4-ce75357e3035" ], "id": "Hya1moGb-", "taskLists": [ "BylTyXiM-b", "BkWpk7jGZb", "H1fTyXjMWW" ] } ], "taskLists": [ { "name": "待辦", "projectId": "Sk2HaTagb", "order": 1, "id": "BkenST66lb" }, { "name": "已完成", "projectId": "Sk2HaTagb", "order": 3, "id": "SkG3Ba6Tgb" }, { "name": "待辦", "projectId": "Hya1moGb-", "order": 1, "id": "BylTyXiM-b" }, { "name": "進行中", "projectId": "Hya1moGb-", "order": 2, "id": "BkWpk7jGZb" }, { "name": "已完成", "projectId": "Hya1moGb-", "order": 3, "id": "H1fTyXjMWW" } ], "tasks": [ { "desc": "吃晚餐", "taskListId": "BkenST66lb", "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "completed": true, "participantIds": [ "BkkDvwee-", "37489e0c-df34-c261-71c4-ce75357e3035" ], "dueDate": null, "reminder": null, "createDate": "2017-05-17T14:10:01.159Z", "priority": 3, "order": 1, "remark": "", "id": 1 }, { "id": 2, "desc": "趕快出發去萬達", "taskListId": "SkG3Ba6Tgb", "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "completed": false, "participantIds": [ "37489e0c-df34-c261-71c4-ce75357e3035" ], "dueDate": "2017-06-12T16:00:00.000Z", "reminder": "2017-07-18T16:00:00.000Z", "createDate": "2017-05-19T10:03:58.794Z", "priority": 2, "order": 2, "remark": "something" }, { "id": 3, "desc": "什麼情況啊", "taskListId": "BkenST66lb", "ownerId": "BkkDvwee-", "completed": false, "participantIds": [], "dueDate": "2017-06-15T16:00:00.000Z", "reminder": "2017-06-21T16:00:00.000Z", "createDate": "2017-05-23T08:21:04.445Z", "priority": 1, "remark": "" }, { "taskListId": "BylTyXiM-b", "desc": "登入鑑權過程中需攜帶 token 訪問 API", "completed": false, "ownerId": "BkkDvwee-", "participantIds": [], "dueDate": "2017-07-03T16:00:00.000Z", "priority": 2, "remark": "", "reminder": "2017-06-27T16:00:00.000Z", "createDate": "2017-05-24T06:11:05.625Z", "id": 4 }, { "taskListId": "BkWpk7jGZb", "desc": "增加統計報表功能", "completed": false, "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "participantIds": [], "dueDate": "2017-07-25T16:00:00.000Z", "priority": 1, "remark": "", "reminder": "2017-07-17T16:00:00.000Z", "createDate": "2017-05-24T06:11:48.513Z", "id": 5 }, { "taskListId": "BylTyXiM-b", "desc": "首頁 banner 增加 2px 的 margin", "completed": false, "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "participantIds": [ "BkkDvwee-" ], "dueDate": "2017-06-29T16:00:00.000Z", "priority": 3, "remark": "", "reminder": null, "createDate": "2017-05-24T06:12:59.718Z", "id": 6 }, { "taskListId": "BkWpk7jGZb", "desc": "庫存管理 -- 出庫操作 API", "completed": false, "ownerId": "BkkDvwee-", "participantIds": [ "37489e0c-df34-c261-71c4-ce75357e3035" ], "dueDate": "2017-07-24T16:00:00.000Z", "priority": 2, "remark": "", "reminder": "2017-07-17T16:00:00.000Z", "createDate": "2017-05-24T06:14:47.575Z", "id": 7 }, { "taskListId": "H1fTyXjMWW", "desc": "使用者可以簽入簽出裝置", "completed": true, "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "participantIds": [], "dueDate": "2017-05-20T16:00:00.000Z", "priority": 1, "remark": "", "reminder": "2017-05-20T16:00:00.000Z", "createDate": "2017-05-24T06:16:24.575Z", "id": 8 }, { "taskListId": "BkWpk7jGZb", "desc": "什麼情況啊?", "completed": false, "ownerId": "37489e0c-df34-c261-71c4-ce75357e3035", "participantIds": [], "dueDate": null, "priority": 3, "remark": "", "reminder": null, "createDate": "2017-06-15T12:03:36.290Z", "id": 9 } ], "quotes": [ { "id": "0", "cn": "我突然就覺得自己像個華麗的木偶,演盡了所有的悲歡離合,可是背上總是有無數閃亮的銀色絲線,操縱我哪怕一舉手一投足。", "en": "I suddenly feel myself like a doll,acting all kinds of joys and sorrows.There are lots of shining silvery thread on my back,controlling all my action.", "pic": "/assets/img/quotes/0.jpg" }, { "id": "1", "cn": "被擊垮通常只是暫時的,但如果你放棄的話,就會使它成為永恆。(Marilyn vos Savant)", "en": "Being defeated is often a temporary condition. Giving up is what makes it permanent.", "pic": "/assets/img/quotes/1.jpg" }, { "id": "2", "cn": "不要只因一次挫敗,就放棄你原來決心想達到的夢想。(莎士比亞)", "en": "Do not, for one repulse, forgo the purpose that you resolved to effect.", "pic": "/assets/img/quotes/2.jpg" }, { "id": "3", "cn": "想有發現就要實驗,這項實驗需要時間。—《神盾局特工》", "en": "Discovery requires experimentation, and this experiment will take time.", "pic": "/assets/img/quotes/3.jpg" }, { "id": "4", "cn": "這世界並不會在意你的自尊,這世界希望你在自我感覺良好之前先要有所成就。", "en": "The world won't care about your self-esteem. The world will expect you to accomplish something before you feel good about yourself.", "pic": "/assets/img/quotes/4.jpg" }, { "id": "5", "cn": "當你最終放開了過去,更好的事就會到來。", "en": "When you finally let go of the past, something better comes along.", "pic": "/assets/img/quotes/5.jpg" }, { "id": "6", "cn": "我們學著放開過去傷害我們的人和事,學著只向前看。因為生活本來就是一往直前的。", "en": "We learn to let go of things and people that hurt us in the past and just move on. For life is all about moving on.", "pic": "/assets/img/quotes/6.jpg" }, { "id": "7", "cn": "絕不要因為怕辛苦而拒絕一個想法、夢想或是目標,成功的路上難免伴隨辛苦。(Bob Proctor)", "en": "Never reject an idea, dream or goal because it will be hard work. Success rarely comes without it.", "pic": "/assets/img/quotes/7.jpg" }, { "id": "8", "cn": "我們在人生中會作出許多選擇,帶著這些選擇繼續生活,才是人生中最難的一課。《妙筆生花》", "en": "We all make our choices in life. The hard thing to do is live with them.", "pic": "/assets/img/quotes/8.jpg" }, { "id": "9", "cn": "我總是對新的一天充滿喜悅,這是一次新的嘗試、一個新的開始,翹首以待,黎明之後或是驚喜。(約翰·博因頓·普里斯特利)", "en": "I have always been delighted at the prospect of a new day, a fresh try, one more start, with perhaps a bit of magic waiting somewhere behind the morning.", "pic": "/assets/img/quotes/9.jpg" } ] }
json-server ./mock/data.json
API server啟動在localhost:3000。支援GET,POST,PUT,PATCH,DELETE等REST命令。
每一個json陣列對應一個表
資源以名詞形式作為API路徑。
二、測試REST API
瀏覽器測試get,但不能測試post,put, delete等。
幾種常見的API測試工具
1、使用Postman測試常用API
下載:https://www.getpostman.com/
安裝成功如下:
Get操作很簡單,輸入url,點Send即可。
Post操作需要在header中指明引數。
- 指明Content-Type是application.json
- Body中給出一個json物件
伺服器返回剛才建立的物件。
json server強大的地方是,可以不傳id,會自動生成。因為真實開放中也是在伺服器生成ID的。
json檔案中的資料也會被更新。
2、使用VSCode的REST Client外掛
建立一個字尾名是.http的檔案rest.http
REST Client用純文字描述請求,POST Man是有圖形介面的。
發出一個post請求到 http://localhost:8090/auth,它的Content-Type是什麼型別,然後攜帶的json是什麼。
注意:
- 在json和http頭部上面有一個空行
- 需要用###將不同的請求隔離開
// Login POST http://localhost:8090/auth Content-Type: application/json;charset: utf-8 { "username":"peng1", "password":"peng1234" } ###
本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載註明出處:https://www.cnblogs.com/starof/p/9068983.html 有問題歡迎與我討論,共同進步。