index.html

<div>
	<label for="user-id-input">user id</label>
	<input type="number" id="user-id-input">
</div>

<button id="b1">get all</button>
<button id="b2">get one</button>
<button id="b3">insert avi biter</button>
<button id="b4">delete</button>
<button id="b5">update</button>
1
2
3
4
5
6
const userIdInput = document.querySelector('#user-id-input');
document.querySelector('#b1').addEventListener('click', printAllUsers);
document.querySelector('#b2').addEventListener('click', printSingleUser);
document.querySelector('#b3').addEventListener('click', insertAviBiter);
document.querySelector('#b4').addEventListener('click', deleteChosenUser);
document.querySelector('#b5').addEventListener('click', updateAviDifferentEmail);

Get All Users

1
2
3
4
5
6
async function printAllUsers() {
    const apiUrl = 'http://localhost:3000/users';
    const res = await fetch(apiUrl);
    const data = await res.json();
    console.log(data);
}

Get One User By ID

1
2
3
4
5
6
7
async function printSingleUser() {
    const userId = userIdInput.value;
    const apiUrl = `http://localhost:3000/users/${userId}`;
    const res = await fetch(apiUrl);
    const data = await res.json();
    console.log(data);
}

Insert Avi Biter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function insertAviBiter() {
    const apiUrl = 'http://localhost:3000/users';
    const reqBody = {
        name: 'avi biter',
        email: 'avi@gmail.com',
    };
    const options = {
        body: JSON.stringify(reqBody),
        headers: {'Content-Type': 'application/json'},
        method: 'POST'
    };
    fetch(apiUrl, options);
}

Delete User By ID

1
2
3
4
5
6
function deleteChosenUser() {
    const userId = userIdInput.value;
    const apiUrl = `http://localhost:3000/users/${userId}`;
    const options = { method: 'DELETE' };
    fetch(apiUrl, options);
}

Update User With ID 4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function updateAviDifferentEmail() {
    const apiUrl = `http://localhost:3000/users/4`;
    const reqBody = { email: 'avi@walla.com' };
    const options = {
        body: JSON.stringify(reqBody),
        headers: {'Content-Type': 'application/json'},
        method: 'PATCH'
    };
    fetch(apiUrl, options);
}