-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (120 loc) · 3.34 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const e = require('express');
const fs = require('fs');
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
const data = fs.readFileSync('./datastore/data.json');
let projects = JSON.parse(data);
app.use(express.json());
app.use('/',express.static(__dirname + '/public'))
// app.get('/page',(req,res)=>{
// try{
// res.sendFile(path.join(__dirname,'/abc.html'));
// // res.end();
// }
// catch(e){
// const message = { message: 'error in reading file !' };
// res.status(400).json(message);
// }
// })
// app.get(/\/.*\.js/,(req,res)=>{
// console.log(req.url);
// res.status(200).send({msg:'received'})
// })
app.get('/api',(req,res)=>{
console.log(req.query);
if(req.query.id)
{
if(projects[req.query.id])
{
res.status(200).json({...projects[req.query.id],id:id})
}
else{
res.status(400).json({message:'No content with the given id'});
}
}
else{
res.status(200).json(projects)
}
})
app.put('/api',async(req,res)=>{
let id= req.query.id;
console.log(req.body);
if(id){
if(projects[id]){
if(req.body.content)
{
projects[id] = {content:req.body.content};
let sproject = JSON.stringify(projects);
try{
await fs.promises.writeFile('./datastore/data.json',sproject)
res.json(projects);
}
catch(err){
res.status(500).json({message:'Could not persist data'})
}
}
else{
res.status(400).json({message:'No valid content'})
}
}
else{
res.status(400).json({message:'No content with this id'})
}
}
else{
res.status(400).json({message:'No valid id'})
}
})
app.post('/api',async(req,res)=>{
let {id,content} = req.body;
if(id && content)
{
if(projects[id])
{
res.status(400).json({message:'id not unique'})
}
else{
projects = {...projects, [id]:{content:content}};
let sproject = JSON.stringify(projects);
try{
await fs.promises.writeFile('./datastore/data.json',sproject);
res.status(200).json(projects);
}
catch(err)
{
res.status(400).json({message:'Could not persist data'});
}
}
}
else{
res.status(400).json({message:'No id or content'});
}
})
app.delete('/api',async(req,res)=>{
if(req.query.id)
{
if(projects[req.query.id])
{
delete projects[req.query.id];
let sproject = JSON.stringify(projects);
try{
await fs.promises.writeFile('./datastore/data.json',sproject)
res.status(200).json({message:'Deleted'});
}
catch(err){
res.status(400).json({message:'Could not persist data'});
}
}
else{
res.status(400).json({message:'No content with this id'});
}
}
else{
res.status(400).json({message:'No valid id'});
}
})
app.listen(port,()=>{
console.log('server is running');
})