Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -37,12 +37,28 @@ if (!fs.existsSync(SNAPSHOT_DIR)) {
|
|
| 37 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 38 |
|
| 39 |
// ============================================================
|
| 40 |
-
// 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
// ============================================================
|
| 42 |
app.get('/snapshots/:filename', (req, res) => {
|
| 43 |
const filename = req.params.filename;
|
| 44 |
|
| 45 |
-
// Security: prevent directory traversal
|
| 46 |
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
|
| 47 |
console.warn(`[SERVER] Blocked malicious filename: ${filename}`);
|
| 48 |
return res.status(400).send('Invalid filename');
|
|
@@ -68,7 +84,7 @@ app.get('/snapshots/:filename', (req, res) => {
|
|
| 68 |
});
|
| 69 |
|
| 70 |
// ============================================================
|
| 71 |
-
//
|
| 72 |
// ============================================================
|
| 73 |
app.post('/api/log', (req, res) => {
|
| 74 |
try {
|
|
@@ -83,7 +99,7 @@ app.post('/api/log', (req, res) => {
|
|
| 83 |
});
|
| 84 |
|
| 85 |
// ============================================================
|
| 86 |
-
//
|
| 87 |
// ============================================================
|
| 88 |
app.post('/api/snapshot', (req, res) => {
|
| 89 |
try {
|
|
@@ -136,7 +152,7 @@ app.post('/api/snapshot', (req, res) => {
|
|
| 136 |
});
|
| 137 |
|
| 138 |
// ============================================================
|
| 139 |
-
//
|
| 140 |
// ============================================================
|
| 141 |
app.get('/api/snapshots/list', (req, res) => {
|
| 142 |
try {
|
|
@@ -153,7 +169,7 @@ app.get('/api/snapshots/list', (req, res) => {
|
|
| 153 |
});
|
| 154 |
|
| 155 |
// ============================================================
|
| 156 |
-
//
|
| 157 |
// ============================================================
|
| 158 |
app.delete('/api/snapshots/:filename', (req, res) => {
|
| 159 |
const filename = req.params.filename;
|
|
@@ -178,14 +194,14 @@ app.delete('/api/snapshots/:filename', (req, res) => {
|
|
| 178 |
});
|
| 179 |
|
| 180 |
// ============================================================
|
| 181 |
-
//
|
| 182 |
// ============================================================
|
| 183 |
app.get('/health', (req, res) => {
|
| 184 |
res.status(200).send('OK');
|
| 185 |
});
|
| 186 |
|
| 187 |
// ============================================================
|
| 188 |
-
//
|
| 189 |
// ============================================================
|
| 190 |
app.listen(PORT, () => {
|
| 191 |
console.log(`[SERVER] Running on port ${PORT}`);
|
|
|
|
| 37 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 38 |
|
| 39 |
// ============================================================
|
| 40 |
+
// 4. Explicit route for admin.html (case-insensitive fallback)
|
| 41 |
+
// ============================================================
|
| 42 |
+
app.get('/admin.html', (req, res) => {
|
| 43 |
+
const adminPath = path.join(__dirname, 'public', 'admin.html');
|
| 44 |
+
const altPath = path.join(__dirname, 'public', 'Admin.html');
|
| 45 |
+
|
| 46 |
+
if (fs.existsSync(adminPath)) {
|
| 47 |
+
res.sendFile(adminPath);
|
| 48 |
+
} else if (fs.existsSync(altPath)) {
|
| 49 |
+
console.log('[SERVER] Serving Admin.html (uppercase) as admin.html');
|
| 50 |
+
res.sendFile(altPath);
|
| 51 |
+
} else {
|
| 52 |
+
res.status(404).send('admin.html not found. Please create the file in the public folder.');
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
// ============================================================
|
| 57 |
+
// 5. Snapshot Binary Endpoint
|
| 58 |
// ============================================================
|
| 59 |
app.get('/snapshots/:filename', (req, res) => {
|
| 60 |
const filename = req.params.filename;
|
| 61 |
|
|
|
|
| 62 |
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
|
| 63 |
console.warn(`[SERVER] Blocked malicious filename: ${filename}`);
|
| 64 |
return res.status(400).send('Invalid filename');
|
|
|
|
| 84 |
});
|
| 85 |
|
| 86 |
// ============================================================
|
| 87 |
+
// 6. POST /api/log β Receive frontend logs
|
| 88 |
// ============================================================
|
| 89 |
app.post('/api/log', (req, res) => {
|
| 90 |
try {
|
|
|
|
| 99 |
});
|
| 100 |
|
| 101 |
// ============================================================
|
| 102 |
+
// 7. POST /api/snapshot β Save New Snapshot
|
| 103 |
// ============================================================
|
| 104 |
app.post('/api/snapshot', (req, res) => {
|
| 105 |
try {
|
|
|
|
| 152 |
});
|
| 153 |
|
| 154 |
// ============================================================
|
| 155 |
+
// 8. GET /api/snapshots/list β List All Snapshots
|
| 156 |
// ============================================================
|
| 157 |
app.get('/api/snapshots/list', (req, res) => {
|
| 158 |
try {
|
|
|
|
| 169 |
});
|
| 170 |
|
| 171 |
// ============================================================
|
| 172 |
+
// 9. DELETE /api/snapshots/:filename β Delete a Snapshot
|
| 173 |
// ============================================================
|
| 174 |
app.delete('/api/snapshots/:filename', (req, res) => {
|
| 175 |
const filename = req.params.filename;
|
|
|
|
| 194 |
});
|
| 195 |
|
| 196 |
// ============================================================
|
| 197 |
+
// 10. Health Check
|
| 198 |
// ============================================================
|
| 199 |
app.get('/health', (req, res) => {
|
| 200 |
res.status(200).send('OK');
|
| 201 |
});
|
| 202 |
|
| 203 |
// ============================================================
|
| 204 |
+
// 11. Start Server
|
| 205 |
// ============================================================
|
| 206 |
app.listen(PORT, () => {
|
| 207 |
console.log(`[SERVER] Running on port ${PORT}`);
|