Proxy
This commit is contained in:
@@ -22,4 +22,5 @@ export const env = {
|
||||
RABBITMQ_SECURE: process.env.RABBITMQ_SECURE,
|
||||
RABBITMQ_RETRY_INTERVAL: process.env.RABBITMQ_INTERVAL,
|
||||
RABBITMQ_VHOST: String(process.env.RABBITMQ_VHOST),
|
||||
CONNECTIONS_URL: String(process.env.CONNECTIONS_URL)
|
||||
};
|
||||
|
||||
@@ -1,21 +1,58 @@
|
||||
import { env } from "#config/env/index.js"
|
||||
import { Router } from "express"
|
||||
import { ClientRequest, IncomingMessage } from "http"
|
||||
import { createProxyMiddleware } from "http-proxy-middleware"
|
||||
import { Request } from "express"
|
||||
|
||||
export const connectionsRoutes = Router()
|
||||
|
||||
const CONNECTIONSURL = "" // TODO: Meter al ENV
|
||||
const CONNECTIONS_URL = env.CONNECTIONS_URL// TODO: Meter al ENV
|
||||
//const CONNECTIONS_URL = "http://sf-nfc-server.savefamilygps.net"
|
||||
|
||||
connectionsRoutes.post("/", createProxyMiddleware({
|
||||
target: CONNECTIONSURL + "/sim/activate",
|
||||
console.log("CONNURL: ", CONNECTIONS_URL)
|
||||
|
||||
connectionsRoutes.use("", createProxyMiddleware({
|
||||
target: CONNECTIONS_URL,
|
||||
changeOrigin: true,
|
||||
// pathrewrite
|
||||
//on proxy req
|
||||
pathRewrite: {
|
||||
'^/': "/simconnections/"
|
||||
},
|
||||
on: {
|
||||
proxyReq: (proxyReq: ClientRequest, req: Request) => {
|
||||
// 1. Construimos la URL de origen (lo que recibe tu proxy)
|
||||
console.log(JSON.stringify(req))
|
||||
const protocol = req.protocol;
|
||||
const host = req.get('host');
|
||||
const originalFullUrl = `${protocol}://${host}${req.originalUrl}`;
|
||||
|
||||
// 2. Construimos la URL de destino (hacia donde se envía)
|
||||
// proxyReq.path ya incluye el path rewrite y los query params
|
||||
const destinationFullUrl = `${CONNECTIONS_URL}${proxyReq.path}`;
|
||||
|
||||
console.log('──────────────────────────────────────────────────');
|
||||
console.log(`[PROXY_DEBUG]`);
|
||||
console.log(` ENTRADA: ${originalFullUrl}`);
|
||||
console.log(` MÉTODO : ${req.method}`);
|
||||
console.log(` DESTINO: ${destinationFullUrl}`);
|
||||
console.log('──────────────────────────────────────────────────');
|
||||
|
||||
//console.log(`[Proxy Req]: ${req.method} ${req.url} -> ${proxyReq.path}`);
|
||||
},
|
||||
proxyRes: (proxyRes, req, res) => {
|
||||
console.log(`[Proxy Res] Status: ${proxyRes.statusCode} desde ${req.url}`);
|
||||
},
|
||||
error: (err, req, res) => {
|
||||
console.error('[Proxy Error]:', err);
|
||||
|
||||
// Validamos que 'res' tenga el método 'status' (típico de Express Response)
|
||||
if ('status' in res) {
|
||||
//@ts-ignore
|
||||
res.status(500).json({ message: 'Error interno en el Gateway' });
|
||||
}
|
||||
},
|
||||
}
|
||||
}))
|
||||
|
||||
connectionsRoutes.post("/sim/pause", createProxyMiddleware({
|
||||
target: CONNECTIONSURL + "/sim/pause",
|
||||
changeOrigin: true
|
||||
}))
|
||||
|
||||
|
||||
// Rutas
|
||||
|
||||
Reference in New Issue
Block a user