An warware: json uwar garken

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.

Shafi posts:

Leave a Comment