Skip to content

Instantly share code, notes, and snippets.

@Pal-Sandeep
Created January 5, 2024 03:48
Show Gist options
  • Save Pal-Sandeep/dd922a22da4d58ce92c3756b8b20bac9 to your computer and use it in GitHub Desktop.
Save Pal-Sandeep/dd922a22da4d58ce92c3756b8b20bac9 to your computer and use it in GitHub Desktop.
curd with express
// const fs = require('fs');
const express = require('express');
const app = express();
const port = 3001
app.use(express.json());
const users = [{
name:"john",
kidney: [{
healthy: false
}]
}]
app.get('/', function (req,res) {
const johnkidney = users[0].kidney;
const numberOfKidneys = johnkidney.length;
let noOfHealthyKidneys = 0;
for (let i = 0; i < numberOfKidneys; i++) {
if (johnkidney[i].healthy) {
noOfHealthyKidneys++
}
}
console.log(johnkidney);
console.log(numberOfKidneys);
console.log(noOfHealthyKidneys);
// res.send('hello 123')
res.json({
noOfHealthyKidneys,
numberOfKidneys,
johnkidney
})
})
app.post('/post', function (req,res) {
const isHealthy = req.body.isHealthy;
console.log(isHealthy);
users[0].kidney.push({
healthy: isHealthy
});
res.send('your data has been added')
})
app.put('/', function (req,res) {
for (let i = 0; i < users[0].kidney.length; i++) {
if (users[0].kidney[i].healthy) {
users[0].kidney[i].healthy = false
} else {
users[0].kidney[i].healthy = true
}
}
res.send('your data has been updated')
})
app.delete('/', function (req,res) {
const newKeyneys = [];
for (let i = 0; i < users[0].kidney.length; i++) {
if (users[0].kidney[i].healthy) {
newKeyneys.push(users[0].kidney[i])
}
}
users[0].kidney = newKeyneys;
res.json({message:'success'})
})
app.listen(port, () => console.log(`listening on port ${port}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment