-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (49 loc) · 2 KB
/
server.js
File metadata and controls
60 lines (49 loc) · 2 KB
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
const express = require('express');
const path = require('path');
const { scrapeExercises } = require('./scraper');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.post('/api/scrape', async (req, res) => {
// SSE Headers
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
const { databaseName, sectionName, headless } = req.body;
if (!databaseName || !sectionName) {
res.write(`data: ${JSON.stringify({ type: 'error', message: 'Faltan parámetros' })}\n\n`);
return res.end();
}
const sendLog = (message) => {
res.write(`data: ${JSON.stringify({ type: 'log', message })}\n\n`);
};
try {
sendLog(`Inicializando extracción web para ${databaseName} - ${sectionName}...`);
const filename = await scrapeExercises({
databaseName,
sectionName,
onProgress: sendLog,
// Override headless via API if needed, otherwise default to true for web
headless: headless !== undefined ? headless : true
});
res.write(`data: ${JSON.stringify({ type: 'success', message: '¡Extracción completada!', filename })}\n\n`);
res.end();
} catch (error) {
sendLog(`ERROR FATAL: ${error.message}`);
res.write(`data: ${JSON.stringify({ type: 'error', message: error.message })}\n\n`);
res.end();
}
});
app.get('/api/download/:filename', (req, res) => {
const filename = req.params.filename;
// VERY BASIC security check to prevent directory traversal
if (filename.includes('/') || filename.includes('\\')) {
return res.status(403).send('Invalid filename');
}
const filepath = path.join(__dirname, filename);
res.download(filepath);
});
app.listen(PORT, () => {
console.log(`🚀 Servidor ejecutándose en http://localhost:${PORT}`);
});