forked from petrk94/smsviewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
75 lines (59 loc) · 2.29 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const port = 3000; // Porta fissa per il server
// Debug: Log server start
console.log('Starting server...');
// Serve la cartella 'public' per il contenuto statico (HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'public')));
// La directory in cui sono salvati i file XML
const smsDirectory = path.join(__dirname, 'sms');
// Funzione per ottenere il file XML più recente
function getLatestXmlFile() {
// Debug: Log directory read
console.log('Reading SMS directory:', smsDirectory);
const files = fs.readdirSync(smsDirectory);
const xmlFiles = files.filter(file => file.endsWith('.xml'));
if (xmlFiles.length === 0) {
// Debug: No XML files found
console.log('No XML files found in SMS directory');
return null;
}
// Ordina i file per data di modifica, in modo da ottenere il più recente
const latestFile = xmlFiles.sort((a, b) => {
return fs.statSync(path.join(smsDirectory, b)).mtime - fs.statSync(path.join(smsDirectory, a)).mtime;
})[0];
// Debug: Log latest file found
console.log('Latest XML file:', latestFile);
return latestFile;
}
// Endpoint per ottenere il contenuto del file XML più recente
app.get('/get-latest-sms', (req, res) => {
const latestFile = getLatestXmlFile();
if (!latestFile) {
// Debug: No SMS backup files found
console.log('No SMS backup files found');
return res.status(404).send('No SMS backup files found');
}
const filePath = path.join(smsDirectory, latestFile);
// Leggi il file e invialo come risposta
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
// Debug: Error reading the SMS file
console.error('Error reading the SMS file:', err);
return res.status(500).send('Error reading the SMS file');
}
// Debug: Successfully read the SMS file
console.log('Successfully read the SMS file');
res.type('xml').send(data); // Risponde con il contenuto del file XML
});
});
// Servire l'index.html per tutte le altre richieste
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Avvia il server sulla porta configurata
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});