Files
api/server.js
2026-02-07 16:13:51 +00:00

52 lines
1.4 KiB
JavaScript

import express from "express";
import cors from "cors";
const app = express();
app.use(cors());
app.use(express.json());
// --- “BD” en memoria (se borra al redeploy/restart) ---
const services = [];
let nextId = 1;
app.get("/", (req, res) => res.send("IntegraRepara API OK"));
app.get("/health", (req, res) => {
res.json({ ok: true, db: false, now: new Date().toISOString() });
});
// GET /services
app.get("/services", (req, res) => {
const list = [...services].sort((a, b) => b.id - a.id);
res.json({ ok: true, count: list.length, services: list });
});
// POST /services
app.post("/services", (req, res) => {
// Tu HTML manda clientName, aquí lo acepto
const { title, client, clientName, phone, address, notes } = req.body || {};
const clientFinal = client ?? clientName;
if (!title || !clientFinal) {
return res.status(400).json({
ok: false,
error: "Faltan campos obligatorios: title y client (o clientName)",
});
}
const item = {
id: nextId++,
title: String(title).trim(),
client: String(clientFinal).trim(),
phone: phone ? String(phone).trim() : "",
address: address ? String(address).trim() : "",
notes: notes ? String(notes).trim() : "",
createdAt: new Date().toISOString(),
};
services.push(item);
res.status(201).json({ ok: true, service: item });
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`🚀 API listening on :${port}`));