33 lines
962 B
TypeScript
33 lines
962 B
TypeScript
import { Router, Request } from "express"
|
|
import { ClientRequest, } from "http"
|
|
import { createProxyMiddleware } from "http-proxy-middleware"
|
|
import { env } from "#config/env/index.js"
|
|
import assert from "node:assert"
|
|
|
|
const portugalRoutes = Router()
|
|
|
|
const PORTUGAL_URL = env.NOS_CONSUMER_URL
|
|
assert.ok(PORTUGAL_URL, "No se ha definido una URL para el servicio consumidor de Francia")
|
|
|
|
portugalRoutes.use("", createProxyMiddleware({
|
|
target: PORTUGAL_URL,
|
|
changeOrigin: true,
|
|
pathRewrite: {
|
|
'^/spain/*': '/'
|
|
},
|
|
|
|
on: {
|
|
proxyReq: (proxyReq: ClientRequest, req: Request) => {
|
|
/* Debug de las peticiones */
|
|
const protocol = req.protocol;
|
|
const host = req.get('host');
|
|
const originalFullUrl = `${protocol}://${host}${req.originalUrl}`;
|
|
const destinationFullUrl = `${PORTUGAL_URL}${proxyReq.path}`;
|
|
console.log(`[Proxy PT]: ${req.method} ${req.url} -> ${proxyReq.path}`);
|
|
}
|
|
}
|
|
}
|
|
))
|
|
|
|
export { portugalRoutes }
|