-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (50 loc) · 1.45 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const app = new express();
const fs = require('fs');
const data = require('./dataset.json');
app.use(express.json());
app.get('/hospital',(req,res)=>{
res.send(data);
})
app.post('/hospital',(req,res)=>{
data.push(req.body);
fs.writeFile('dataset.json',JSON.stringify(data),(err,resp)=>{
if(err){
res.send("Data can not be written");
}
else{
res.send("Data written successfully");
}
})
})
app.put('/hospital/:name',(req,res)=>{
let name = req.params.name;
data.forEach((item)=>{
if(item.hospitalName == name){
item.hospitalLocation = req.body.hospitalLocation;
item.patientCount = req.body.patientCount;
}
})
fs.writeFile('dataset.json',JSON.stringify(data),(err,resp)=>{
if(err){
res.send("data could not be updated");
}
else{
res.send("Data updated successfully");
}
})
})
app.delete('/hospital/:name',(req,res)=>{
let name = req.params.name;
let value = data.filter(item => item.hospitalName !== name);
fs.writeFile('dataset.json',JSON.stringify(value),(err,resp)=>{
if(err){
res.send("Data can not be deleted");
}
else{
res.send("Data deleted");
}
})
})
app.listen(3000);
console.log("server listening to port 3000");