mirror of
https://github.com/aleleba/run-app-from-cdn.git
synced 2025-07-05 09:48:11 -06:00
PR-824002: Using proxy instead of a redirect.
This commit is contained in:
52
src/app.ts
52
src/app.ts
@ -1,17 +1,61 @@
|
||||
import express from 'express';
|
||||
import dotenv from 'dotenv';
|
||||
import { setRoutes } from './routes/index';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
import type { Request, Response } from 'express';
|
||||
import { NextFunction } from 'http-proxy-middleware/dist/types';
|
||||
import { ClientRequest } from 'http';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const cdnUrl = process.env.CDN_URL;
|
||||
|
||||
// Configuración del middleware de proxy
|
||||
const proxyOptions = {
|
||||
target: cdnUrl, // URL del CDN
|
||||
changeOrigin: true, // Cambiar el origen para evitar problemas de CORS,
|
||||
pathRewrite: (path: string) => path.replace("/", ""),
|
||||
onProxyReq: (proxyReq: ClientRequest, req: Request, res: Response) => {
|
||||
console.log(`Proxying request: ${req.url}`);
|
||||
},
|
||||
onError: (err: any, req: Request, res: Response) => {
|
||||
console.error('Proxy error:', err);
|
||||
if (!cdnUrl) {
|
||||
res.status(500).send('CDN URL not configured');
|
||||
throw new Error('CDN URL not configured');
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Crear el middleware proxy
|
||||
const proxyMiddleware = createProxyMiddleware(proxyOptions);
|
||||
|
||||
// Middlewares para manejar JSON y formularios
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
setRoutes(app);
|
||||
// Middleware para validar si cdnUrl está configurado
|
||||
const validateCdnUrl = (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!cdnUrl) {
|
||||
res.status(500).send('CDN URL not configured');
|
||||
throw new Error('CDN URL not configured');
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
app.listen(PORT, () => {
|
||||
// Usar el middleware de validación antes del proxy
|
||||
app.use(validateCdnUrl);
|
||||
// Configurar el middleware proxy
|
||||
app.use('/', proxyMiddleware);
|
||||
|
||||
// DO NOT DO app.listen() unless we're testing this directly
|
||||
if (require.main === module) {
|
||||
// Iniciar el servidor
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Servidor corriendo en http://localhost:${PORT}`);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Instead do export the app:
|
||||
export default app;
|
||||
|
@ -1,14 +0,0 @@
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
export const getIndex = async (req: Request, res: Response): Promise<void> => {
|
||||
try {
|
||||
const cdnUrl = process.env.CDN_URL;
|
||||
if (!cdnUrl) {
|
||||
res.status(500).send('CDN URL not configured');
|
||||
return;
|
||||
}
|
||||
res.redirect(cdnUrl);
|
||||
} catch (error) {
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
};
|
@ -1,8 +0,0 @@
|
||||
import { Application, Request, Response } from 'express';
|
||||
import { getIndex } from '../controllers/index';
|
||||
|
||||
export function setRoutes(app: Application) {
|
||||
app.get('/', (req: Request, res: Response) => {
|
||||
getIndex(req, res);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user