Actualizar server.js
This commit is contained in:
55
server.js
55
server.js
@@ -1,31 +1,52 @@
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
import pkg from "pg";
|
||||
|
||||
dotenv.config();
|
||||
const { Pool } = pkg;
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
if (!process.env.DATABASE_URL) {
|
||||
console.error("❌ Missing DATABASE_URL");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
||||
// --- “BD” en memoria (se borra cuando redeploy/restart) ---
|
||||
const services = [];
|
||||
let nextId = 1;
|
||||
|
||||
// Raíz
|
||||
app.get("/", (req, res) => res.send("IntegraRepara API OK"));
|
||||
|
||||
app.get("/health", async (req, res) => {
|
||||
try {
|
||||
const r = await pool.query("select now() as now");
|
||||
res.json({ ok: true, db: true, now: r.rows[0].now });
|
||||
} catch (e) {
|
||||
res.status(500).json({ ok: false, db: false, error: e.message });
|
||||
// Health (sin DB por ahora)
|
||||
app.get("/health", (req, res) => {
|
||||
res.json({ ok: true, db: false, now: new Date().toISOString() });
|
||||
});
|
||||
|
||||
// Listar servicios
|
||||
app.get("/services", (req, res) => {
|
||||
// opcional: ordenar por id desc para ver lo último arriba
|
||||
const list = [...services].sort((a, b) => b.id - a.id);
|
||||
res.json({ ok: true, count: list.length, services: list });
|
||||
});
|
||||
|
||||
// Crear servicio
|
||||
app.post("/services", (req, res) => {
|
||||
const { title, client, phone, address, notes } = req.body || {};
|
||||
|
||||
if (!title || !client) {
|
||||
return res.status(400).json({
|
||||
ok: false,
|
||||
error: "Faltan campos obligatorios: title y client",
|
||||
});
|
||||
}
|
||||
|
||||
const item = {
|
||||
id: nextId++,
|
||||
title: String(title).trim(),
|
||||
client: String(client).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;
|
||||
|
||||
Reference in New Issue
Block a user