-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (88 loc) · 3.05 KB
/
server.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
const express = require('express');
const cors = require('cors');
const app = express();
const path = require('path');
const fs = require('fs');
// Enable CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE'); // Add 'DELETE' to the allowed methods
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// app.use(cors());
// Parse JSON bodies
app.use(express.json());
// Serve static files
app.use(express.static(path.join(__dirname)));
app.get("/", (request, response) => {
response.json({ info: "Node.js, Express and meeting API" });
});
// Define a route to serve data.json
app.get('/data.json', (req, res) => {
res.sendFile(path.join(__dirname, 'data.json'));
});
app.post('/addreminder', (req, res) => {
console.log('Received addreminder request');
const newData = req.body;
const dataPath = path.join(__dirname, 'data.json');
fs.readFile(dataPath, 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Failed to read data file' });
return;
}
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (parseErr) {
console.error(parseErr);
res.status(500).json({ error: 'Failed to parse data file' });
return;
}
jsonData.push(newData);
const updatedData = JSON.stringify(jsonData);
fs.writeFile(dataPath, updatedData, 'utf8', (writeErr) => {
if (writeErr) {
console.error(writeErr);
res.status(500).json({ error: 'Failed to write data file' });
return;
}
res.json({ message: 'Data added successfully', data: jsonData });
});
});
});
// ...
app.delete('/api/data/delete/:id', (req, res) => {
const id = req.params.id;
console.log('Received delete request for task ID:', id);
// Read the existing data from data.json
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'Internal server error' });
}
let jsonData = JSON.parse(data);
// Find the index of the data item with the specified id
const index = jsonData.findIndex(item => item.id === id);
if (index === -1) {
return res.status(404).json({ error: 'Data not found' });
}
// Remove the data item from the array
jsonData.splice(index, 1);
// Write the updated data back to data.json
fs.writeFile('data.json', JSON.stringify(jsonData), err => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'Internal server error' });
}
// Return a success response
res.json({ message: 'Data deleted successfully' });
});
});
});
const port = 3444;
const hostname = 'localhost'; // Replace with your server's IP address
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});