** json server
npm install -g json-server
Step 2: Create a db.json file with some data
{
“posts”: [
{ “id”: 1, “title”: “learn json-server”, “author”: “Mrinmay Mukherjee” }
],
“comments”: [
{ “id”: 1, “body”: “it's pretty awesome”, “postId”: 1 }
],
“profile”: { “name”: “Mrinmay Mukherjee” }
}
Step 3: Start JSON Server
json-server --watch db.json --port 8000
Examples of GET and POST:
1.GET
fetch(‘http://localhost:8000/posts/')
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)));
2. POST
let data = {
“id”: 10,
“title”: “json-server is not bad. It's great!”,
“author”: “Dwight Shrute”
}
fetch(‘http://localhost:3000/posts/', {
method: “POST”,
headers: {
“Content-Type”: “application/json”,
},
body: JSON.stringify(data),
})
.then(response => response.json())
.catch(error => console.error(‘Error:’, error))
.then(response => console.log(‘Success:’, JSON.stringify(response)));
Yorumlar
Yorum Gönder