-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (60 loc) · 2.18 KB
/
server.js
File metadata and controls
71 lines (60 loc) · 2.18 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
61
62
63
64
65
66
67
68
69
70
71
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
// Resolver la ruta del archivo
let filePath = req.url === '/' ? 'simple.html' : req.url;
filePath = path.join(__dirname, filePath);
// Obtener la extensión del archivo
const ext = path.extname(filePath).toLowerCase();
// Definir MIME types correctos
const mimeTypes = {
'.html': 'text/html; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.wasm': 'application/wasm',
'.json': 'application/json',
'.css': 'text/css; charset=utf-8',
'.ts': 'application/typescript',
'.d.ts': 'application/typescript'
};
// Establecer Content-Type correcto
const contentType = mimeTypes[ext] || 'application/octet-stream';
res.setHeader('Content-Type', contentType);
// CORS headers para WASM
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
// Manejar OPTIONS requests
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Leer y servir el archivo
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(`Error: ${filePath} - ${err.message}`);
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 - Archivo no encontrado\n' + filePath);
return;
}
res.writeHead(200);
res.end(data);
console.log(`✅ ${req.url} (${ext})`);
});
});
const PORT = 8000;
server.listen(PORT, () => {
console.log('\n' + '='.repeat(50));
console.log('🚀 Servidor WebAssembly activo');
console.log('='.repeat(50));
console.log(`📍 URL: http://localhost:${PORT}`);
console.log(`📁 Directorio: ${__dirname}`);
console.log('='.repeat(50) + '\n');
});
server.on('error', (err) => {
console.error('❌ Error del servidor:', err.message);
});