server.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const PORT = 7777;
  5. const server = http.createServer((req, res) => {
  6. let filePath = req.url === '/' ? '/index.html' : req.url;
  7. filePath = path.join(__dirname, 'public', filePath);
  8. const extname = path.extname(filePath);
  9. const contentTypes = {
  10. '.html': 'text/html',
  11. '.css': 'text/css',
  12. '.js': 'application/javascript',
  13. '.json': 'application/json',
  14. '.png': 'image/png',
  15. '.jpg': 'image/jpeg',
  16. '.jpeg': 'image/jpeg',
  17. '.gif': 'image/gif',
  18. '.svg': 'image/svg+xml',
  19. '.ico': 'image/x-icon',
  20. };
  21. const contentType = contentTypes[extname] || 'text/plain';
  22. fs.readFile(filePath, (err, content) => {
  23. if (err) {
  24. if (err.code === 'ENOENT') {
  25. res.writeHead(404, { 'Content-Type': 'text/html' });
  26. res.end('<h1>404 - Página no encontrada</h1>', 'utf-8');
  27. } else {
  28. res.writeHead(500);
  29. res.end('Error interno del servidor');
  30. }
  31. } else {
  32. res.writeHead(200, { 'Content-Type': contentType });
  33. res.end(content, 'utf-8');
  34. }
  35. });
  36. });
  37. server.listen(PORT, () => {
  38. console.log(`Servidor del Festival Armadillos corriendo en http://localhost:${PORT}`);
  39. });