JSON Server babbar kadara ce ga masu shirye-shirye, musamman lokacin da kake son ฦirฦirar API na REST na karya don haษakawa da dalilai na gwaji. Yana bawa mai amfani damar samar da API a cikin ฦasa da minti ษaya. Kafin ci gaba da aiwatarwa, bari mu fahimci abin da JSON Server zai iya yi.
JSON uwar garken yana amfani da sauฦi JavaScript fayil ko fayil JSON don kiyaye ayyukan bayanai kamar GET, POST, PUT, PATCH, da GAME. Yana ba da sassauci ga masu haษakawa yayin da yake aiki tare da fasahar gaba-gaba kamar Angular, React, Vue, da sauransu.
// Installation npm install -g json-server // To start JSON Server json-server --watch db.json
Abubuwan da ake amfani da su na JSON Server
- Yana ba da damar cikakken REST API na jabu tare da sifili-code a cikin ฦดan daฦiฦa kaษan.
- Yana goyan bayan duk mahimman buฦatun HTTP: SAMU, POST, PUT, PATCH, DELETE.
- Yana goyan bayan jinkirin martani kuma yana haifar da bayanai ta amfani da JS.
- Yana ba da saitin baya mai sauri don samfuri da izgili
- Ya haษa da fasali kamar rarrabawa, slicing, tacewa, da binciken cikakken rubutu.
JSON Server a Action
Fara amfani da JSON Server yana da sauฦi sosai. Bayan shigarwa, duk abin da kuke buฦata shine ฦirฦirar fayil ษin JSON wanda zai yi aiki azaman ฦarshen ฦarshen API ษin ku. Kuna iya yin ba'a da bayanan da ke cikin fayil ษin JSON kan yadda kuke gani gabaษaya a cikin bayanan bayanan duniya na ainihi.
{ "users": [ { "id": 1, "name": "John", "email": "john@example.com" }, { "id": 2, "name": "Kane", "email": "kane@example.com" } ], "posts": [ { "id": 1, "title": "json-server", "author": "John" } ], "comments": [ { "id": 1, "body": "It's amazing", "postId": 1 } ], "profile": { "name": "typicode" } }
Fayil ษin JSON na sama yana kafa Database tare da USERS, POSTS, COMMENTS, da PROFILE azaman teburinsa daban-daban. Sabar JSON tana ษaukar kowane maษalli na sama a matsayin ฦarshen ฦarshen.
Samun dama ga bayanan JSON
Za a iya samun damar bayanan JSON akan wurare daban-daban na ฦarshen (wanda kuma aka sani da hanyoyi a cikin yanayin uwar garken). Misali, idan kuna son ganin duk masu amfani, zaku iya buฦatar ฦarshen ฦarshen / masu amfani.
fetch('http://localhost:3000/users') .then(response => response.json()) .then(data => console.log(data));
Anan muna amfani da API ษin javaScript don neman hanyar masu amfani. Sabar za ta ba da amsa tare da bayanan da suka danganci duk masu amfani da muka kafa a cikin fayil ษin JSON a baya.
Don taฦaita shi, Yin amfani da uwar garken JSON azaman izgili REST API don haษakawa zai inganta haษaka aikin haษakar ku. Bugu da ฦari, ba shi da wahala don saitawa da haษawa tare da tsarin ku.