47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const morgan = require('morgan');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
const LOG_FILE = path.join(__dirname, 'detections.json');
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(morgan('dev'));
|
|
app.use(express.json());
|
|
app.use(express.static('public'));
|
|
|
|
// Helper to read/write logs
|
|
const getLogs = () => {
|
|
if (!fs.existsSync(LOG_FILE)) return [];
|
|
const data = fs.readFileSync(LOG_FILE);
|
|
return JSON.parse(data);
|
|
};
|
|
|
|
const saveLog = (log) => {
|
|
const logs = getLogs();
|
|
logs.push({ ...log, id: Date.now(), timestamp: new Date() });
|
|
fs.writeFileSync(LOG_FILE, JSON.stringify(logs, null, 2));
|
|
};
|
|
|
|
// API Routes
|
|
app.get('/api/detections', (req, res) => {
|
|
res.json(getLogs());
|
|
});
|
|
|
|
app.post('/api/detections', (req, res) => {
|
|
const { label, confidence } = req.body;
|
|
if (!label || !confidence) {
|
|
return res.status(400).json({ error: 'Faltan datos de clasificación' });
|
|
}
|
|
saveLog({ label, confidence });
|
|
res.status(201).json({ message: 'Detección guardada correctamente' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Servidor Backend ejecutándose en http://localhost:${PORT}`);
|
|
});
|