-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (38 loc) · 1.49 KB
/
Copy pathserver.js
File metadata and controls
44 lines (38 loc) · 1.49 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
import http from 'http';
import fs from 'fs';
import path from 'path';
const PORT = 3000;
const FILE_PATH = path.join(import.meta.dirname, 'tasa.json');
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
if (req.url === '/tasa.json' && req.method === 'GET') {
fs.readFile(FILE_PATH, 'utf-8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ error: "Archivo tasa.json no encontrado. Asegúrate de ejecutar el poller primero." }));
return;
}
res.writeHead(500);
res.end("Error leyendo archivo");
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(data);
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end("Solo disponible: GET /tasa.json");
}
});
server.listen(PORT, () => {
console.log(`🚀 Servidor Mock de GitHub Pages iniciado en: http://localhost:${PORT}`);
console.log(`🌐 Tu endpoint de prueba es: http://localhost:${PORT}/tasa.json`);
});