-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (75 loc) · 2.23 KB
/
index.js
File metadata and controls
88 lines (75 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import express from "express";
import cors from "cors";
import morgan from "morgan";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
import "./src/database/database.js";
// Routers
import productoRouter from "./src/routes/productos.routes.js";
import usuarioRouter from "./src/routes/usuarios.routes.js";
import pagoRouter from "./src/routes/pagos.routes.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 4001;
// Middlewares
const corsOptions = {
origin: process.env.FRONTEND_URL || "http://localhost:5173",
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
};
app.use(cors(corsOptions));
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Archivos estáticos
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.join(__dirname, "/public")));
// SDK de Mercado Pago
import { MercadoPagoConfig, Preference } from "mercadopago";
import { registrarPedidoEfectivo } from "./src/controllers/pagos.controllers.js";
// Agrega credenciales
const client = new MercadoPagoConfig({
accessToken: process.env.MP_ACCESS_TOKEN,
});
// Route to create preference mp
app.post("/create-preference", (req, res) => {
const preference = new Preference(client);
preference
.create({
body: {
items: [
{
title: "Mi producto",
quantity: 1,
unit_price: 2000,
},
],
},
})
.then((data) =>{
console.log(data);
//Object data contains all information about our preference
res.status(200).json({
preference_id: data.id,
preference_url: data.init_point,
})
})
.catch(()=>{
res.status(500).json({ error: "Error creando la preferencia" });
});
});
// Rutas
app.use("/api/productos", productoRouter);
app.use("/api/usuarios", usuarioRouter);
app.use("/api/pagos", pagoRouter);
// Ruta de prueba
app.get("/", (req, res) => {
res.send("Servidor de Panadería Backend funcionando 🚀");
});
// Iniciar servidor
app.listen(PORT, () => {
console.log(`Servidor escuchando en el puerto ${PORT}`);
console.log("Base de datos conectada");
});