28
loading...
This website collects cookies to deliver better user experience
fetch('http://localhost:5000/add-users', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(newUser)
})
app.post('/add-users', async (req, res) => {
const newUser = req.body // receive data from frontend
const result = await usersCollection.insertOne(newUser); // save data to the database
fetch(`http://localhost:5000/users`)
app.get('/users', async (req, res) => {
const cursor = usersCollection.find({});
const users = await cursor.toArray();
});
const url = `http://localhost:5000/update-user/${userId}`;
fetch(url, {
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(user)
})
<button onClick={() => handleDeleteUser(user._id)} className="btn btn-danger" >delete</button>
- const handleDeleteUser = id => {
const proceed = window.confirm('Are you sure, you want to delete?');
if (proceed) {
const url = `http://localhost:5000/users/${id}`;
fetch(url, {
method: 'DELETE'
})
- res.json(result);