This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
178 lines (143 loc) · 6.33 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const express = require('express');
const path = require('path');
const fs = require('fs');
const multer = require('multer');
const app = express();
const port = 3000;
// Serve static files (CSS, JavaScript, etc.) from the directory where your files are located
app.use(express.static(__dirname));
// Middleware to parse JSON in request body
app.use(express.json());
// Serve HTML files
app.get('/form', (req, res) => {
res.sendFile(path.join(__dirname, 'form.html'));
});
app.get('/dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'dashboard.html'));
});
// Serve students.json from the directory where your files are located
app.get('/students.json', (req, res) => {
const studentsJsonPath = path.join(__dirname, 'students.json');
res.sendFile(studentsJsonPath);
});
// Create a storage engine for multer to specify where to save the uploaded image
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, 'images')); // Save images in the "images" directory
},
filename: (req, file, cb) => {
const studentId = req.params.studentId; // Get studentId from the URL parameter
cb(null, studentId + '.jpg'); // Use studentId as the image filename
},
});
// Create a multer instance with the storage engine
const upload = multer({ storage: storage });
// Handle POST requests to add student image
app.post('/add-student-image/:studentId', upload.single('studentImage'), (req, res) => {
// Image upload is handled by multer, and the uploaded image is saved in the "images" directory
res.status(200).json({ message: 'Student image added successfully' });
});
// Handle POST requests to add a new student
app.post('/add-student', async (req, res) => {
try {
const newStudentData = req.body;
const filePath = path.join(__dirname, 'students.json');
// Read the existing students data from 'students.json'
const data = fs.readFileSync(filePath, 'utf8');
const studentsData = JSON.parse(data);
// Check if the student ID already exists
const idExists = studentsData.some((student) => student.id === newStudentData.id);
if (idExists) {
return res.status(400).json({ error: 'Student ID already exists' });
}
// Add the new student to the data
studentsData.push(newStudentData);
// Write the updated data back to 'students.json'
fs.writeFileSync(filePath, JSON.stringify(studentsData, null, 2));
res.status(201).json({ message: 'Student added successfully' });
} catch (error) {
console.error('Error adding student:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Handle POST requests to update student image
app.post('/update-student-image/:studentId', upload.single('studentImage'), (req, res) => {
// Image upload is handled by multer, and the uploaded image is saved in the "images" directory
res.status(200).json({ message: 'Student image updated successfully' });
});
// Handle POST requests to update student data
app.post('/update-student', (req, res) => {
// Read the updated data from the request body
const updatedStudentData = req.body;
// Read the existing students data from 'students.json'
const filePath = path.join(__dirname, 'students.json');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading student data:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
const studentsData = JSON.parse(data);
// Find and update the student data based on the student ID
const updatedStudentsData = studentsData.map((student) => {
if (student.id === updatedStudentData.id) {
return updatedStudentData;
}
return student;
});
// Write the updated data back to 'students.json'
fs.writeFile(filePath, JSON.stringify(updatedStudentsData, null, 2), (err) => {
if (err) {
console.error('Error updating student data:', err);
res.status(500).json({ error: 'Internal server error' });
} else {
console.log('Student data updated successfully.');
res.status(200).json({ message: 'Student data updated successfully' });
}
});
});
});
// Handle DELETE requests to delete a student and their image
app.delete('/delete-student/:studentId', (req, res) => {
const studentId = req.params.studentId;
// Delete the student from 'students.json'
const filePath = path.join(__dirname, 'students.json');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading student data:', err);
return res.status(500).json({ error: 'Internal server error' });
}
const studentsData = JSON.parse(data);
// Find the index of the student with the given ID
const studentIndex = studentsData.findIndex((student) => student.id === studentId);
if (studentIndex === -1) {
return res.status(404).json({ error: 'Student not found' });
}
// Remove the student from the array
studentsData.splice(studentIndex, 1);
// Write the updated data back to 'students.json'
fs.writeFile(filePath, JSON.stringify(studentsData, null, 2), (err) => {
if (err) {
console.error('Error deleting student data:', err);
return res.status(500).json({ error: 'Internal server error' });
}
// Delete the student's image (assuming the filename is based on the student ID)
const imagePath = path.join(__dirname, 'images', `${studentId}.jpg`);
fs.unlink(imagePath, (err) => {
if (err) {
console.error('Error deleting student image:', err);
}
console.log('Student and image deleted successfully.');
res.status(200).json({ message: 'Student and image deleted successfully' });
});
});
});
});
// Catch-all route for unknown routes
app.use((req, res) => {
res.status(404).send('Not Found');
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});