Wire all routes, services, and middleware into index.js

Import and mount all API route handlers (facturas, articulos, catalogo,
config, print), initialize the SQLite database, start the XML file
watcher, and serve static files with SPA fallback. Add placeholder
index.html for the frontend shell. Use Express 5 '*path' wildcard
syntax for the catch-all route.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
consultoria-as
2026-02-17 06:36:05 +00:00
parent 1eac71790c
commit 9b1aaf2802
2 changed files with 82 additions and 1 deletions

View File

@@ -1,14 +1,47 @@
const express = require('express');
const path = require('path');
const { createDatabase } = require('./services/database');
const { startWatcher } = require('./services/watcher');
const { facturasRouter } = require('./routes/facturas');
const { articulosRouter } = require('./routes/articulos');
const { catalogoRouter } = require('./routes/catalogo');
const { configRouter } = require('./routes/config');
const { printRouter } = require('./routes/print');
const app = express();
const PORT = process.env.PORT || 3000;
const DB_PATH = process.env.DB_PATH || path.join(__dirname, '..', 'data', 'refaccionaria.db');
// Initialize database
const db = createDatabase(DB_PATH);
// Middleware
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// API Routes
app.use('/api/facturas', facturasRouter(db));
app.use('/api/articulos', articulosRouter(db));
app.use('/api/catalogo', catalogoRouter(db));
app.use('/api/config', configRouter(db));
app.use('/api/print', printRouter(db));
// SPA fallback — serve index.html for non-API routes
app.get('*path', (req, res) => {
if (!req.path.startsWith('/api')) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
});
// Start file watcher
const xmlFolder = db.prepare("SELECT valor FROM configuracion WHERE clave = 'xml_folder'").get();
if (xmlFolder) {
const folderPath = path.resolve(xmlFolder.valor);
startWatcher(db, folderPath);
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`Portal running at http://0.0.0.0:${PORT}`);
console.log(`Portal Refaccionaria running at http://0.0.0.0:${PORT}`);
});
module.exports = app;

48
src/public/index.html Normal file
View File

@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Portal Refaccionaria</title>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
<aside class="sidebar" id="sidebar">
<div class="sidebar-header">
<h1 class="sidebar-title">Portal Refaccionaria</h1>
</div>
<nav class="sidebar-nav">
<a href="#facturas" class="nav-link" data-route="facturas">
<span class="nav-icon">&#128196;</span>
<span class="nav-text">Facturas</span>
</a>
<a href="#articulos" class="nav-link" data-route="articulos">
<span class="nav-icon">&#128269;</span>
<span class="nav-text">Art&iacute;culos</span>
</a>
<a href="#catalogo" class="nav-link" data-route="catalogo">
<span class="nav-icon">&#128203;</span>
<span class="nav-text">Cat&aacute;logo</span>
</a>
<a href="#config" class="nav-link" data-route="config">
<span class="nav-icon">&#9881;&#65039;</span>
<span class="nav-text">Configuraci&oacute;n</span>
</a>
</nav>
<div class="sidebar-footer">
<small>v1.0 &mdash; Refaccionaria</small>
</div>
</aside>
<main class="content" id="content">
<div class="loading-screen">
<div class="spinner"></div>
<p>Cargando...</p>
</div>
</main>
<div class="toast-container" id="toastContainer"></div>
<script src="/js/app.js"></script>
</body>
</html>