教學目標:讓找不到好 API 的學生,也能自己寫 API 來練習,找到前端工作
npm install -g json-server
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
GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2
GET /comments?author.name=typicode
page
頁數、limit
一頁幾筆
`GET /posts?_page=7
GET /posts?_page=7&_limit=20`
`GET /posts?_page=7
GET /posts?_page=7&_limit=20`
public
,可放靜態檔案Q:可以拿來當作真正的伺服器嗎?
A:不行,他是記錄在記憶體上,而非複寫檔案
Q:如何確保面試官看的內容都有資料?
A:一開始在 db.json 將全部資料寫上即可
JSON-Server-Auth
/register
、/login
介紹npm install -g json-server json-server-auth express
npm install json-server json-server-auth express
GET:/users/2/bookmarks
POST:/products/1/bookmarks
?_expand=product&_expand=user