|
| 1 | +const express = require("express"); |
| 2 | +const fs = require("fs").promises; |
| 3 | +const path = require("path"); |
| 4 | +const cors = require("cors"); |
| 5 | + |
| 6 | +const app = express(); |
| 7 | +app.use(cors()); |
| 8 | + |
| 9 | +// Servir archivos estáticos desde la carpeta templates |
| 10 | +app.use("/templates", express.static(path.join(__dirname, "public/templates"))); |
| 11 | + |
| 12 | +// Ruta para listar directorios en formato JSON |
| 13 | +app.get("/api/templates", async (req, res) => { |
| 14 | + try { |
| 15 | + const templatesPath = path.join(__dirname, "public/templates"); |
| 16 | + console.log("Buscando templates en:", templatesPath); // Para debug |
| 17 | + |
| 18 | + // Verificar si el directorio existe |
| 19 | + try { |
| 20 | + await fs.access(templatesPath); |
| 21 | + } catch (e) { |
| 22 | + console.error("El directorio no existe:", templatesPath); |
| 23 | + return res.status(500).json({ error: "Directory not found" }); |
| 24 | + } |
| 25 | + |
| 26 | + const dirs = await fs.readdir(templatesPath); |
| 27 | + console.log("Directorios encontrados:", dirs); // Para debug |
| 28 | + |
| 29 | + const templates = await Promise.all( |
| 30 | + dirs.map(async (dir) => { |
| 31 | + const dirPath = path.join(templatesPath, dir); |
| 32 | + const stat = await fs.stat(dirPath); |
| 33 | + |
| 34 | + if (stat.isDirectory()) { |
| 35 | + const files = await fs.readdir(dirPath); |
| 36 | + const filesInfo = await Promise.all( |
| 37 | + files.map(async (file) => { |
| 38 | + const filePath = path.join(dirPath, file); |
| 39 | + const fileStat = await fs.stat(filePath); |
| 40 | + return { |
| 41 | + name: file, |
| 42 | + path: `/templates/${dir}/${file}`, |
| 43 | + size: fileStat.size, |
| 44 | + modified: fileStat.mtime, |
| 45 | + }; |
| 46 | + }) |
| 47 | + ); |
| 48 | + |
| 49 | + return { |
| 50 | + name: dir, |
| 51 | + path: `/templates/${dir}`, |
| 52 | + files: filesInfo, |
| 53 | + }; |
| 54 | + } |
| 55 | + return null; |
| 56 | + }) |
| 57 | + ); |
| 58 | + |
| 59 | + res.json({ |
| 60 | + templates: templates.filter((t) => t !== null), |
| 61 | + }); |
| 62 | + } catch (error) { |
| 63 | + console.error("Error:", error); // Para debug |
| 64 | + res.status(500).json({ error: error.message }); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +// Ruta para obtener información de un template específico |
| 69 | +app.get("/api/templates/:template", async (req, res) => { |
| 70 | + try { |
| 71 | + const templatePath = path.join( |
| 72 | + __dirname, |
| 73 | + "public/templates", |
| 74 | + req.params.template |
| 75 | + ); |
| 76 | + const files = await fs.readdir(templatePath); |
| 77 | + |
| 78 | + const filesInfo = await Promise.all( |
| 79 | + files.map(async (file) => { |
| 80 | + const filePath = path.join(templatePath, file); |
| 81 | + const stat = await fs.stat(filePath); |
| 82 | + return { |
| 83 | + name: file, |
| 84 | + path: `/templates/${req.params.template}/${file}`, |
| 85 | + size: stat.size, |
| 86 | + modified: stat.mtime, |
| 87 | + }; |
| 88 | + }) |
| 89 | + ); |
| 90 | + |
| 91 | + res.json({ |
| 92 | + name: req.params.template, |
| 93 | + path: `/templates/${req.params.template}`, |
| 94 | + files: filesInfo, |
| 95 | + }); |
| 96 | + } catch (error) { |
| 97 | + res.status(500).json({ error: error.message }); |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +// Ruta para debug - muestra la estructura de directorios |
| 102 | +app.get("/debug", (req, res) => { |
| 103 | + const debugInfo = { |
| 104 | + currentDir: __dirname, |
| 105 | + publicTemplatesPath: path.join(__dirname, "public/templates"), |
| 106 | + exists: fs.existsSync(path.join(__dirname, "public/templates")), |
| 107 | + }; |
| 108 | + res.json(debugInfo); |
| 109 | +}); |
| 110 | + |
| 111 | +const PORT = 4000; |
| 112 | +app.listen(PORT, () => { |
| 113 | + console.log(`Server running on port ${PORT}`); |
| 114 | + console.log("Directorio actual:", __dirname); |
| 115 | + console.log("Ruta a templates:", path.join(__dirname, "public/templates")); |
| 116 | +}); |
0 commit comments