From a491514d7080dd8d965c3bc28ef4a376c0ec203d Mon Sep 17 00:00:00 2001 From: MenGonz Date: Mon, 20 Jan 2025 13:48:52 -0500 Subject: [PATCH] trying something else --- frontdoor-endpoints/frontdoor-api.js | 52 ++++++++++++++++++++++++++++ frontdoor-endpoints/routes.js | 15 ++------ index.js | 7 ++-- 3 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 frontdoor-endpoints/frontdoor-api.js diff --git a/frontdoor-endpoints/frontdoor-api.js b/frontdoor-endpoints/frontdoor-api.js new file mode 100644 index 0000000..6d35c59 --- /dev/null +++ b/frontdoor-endpoints/frontdoor-api.js @@ -0,0 +1,52 @@ +const express = require('express'); +const axios = require('axios'); + +const router = express.Router(); + + + +// Middleware: Add APIMS subscription key to outgoing requests +const addSubscriptionKey = (req, res, next) => { + const subscriptionKey = process.env.REACT_APP_APIMS_SUBSCRIPTION_KEY; // Store this key in .env file + if (!subscriptionKey) { + console.error('[Proxy Middleware] Missing APIMS Subscription Key'); + return res.status(500).send('Internal Server Error: Missing APIMS Subscription Key'); + } + req.headers['Ocp-Apim-Subscription-Key'] = subscriptionKey; + next(); +}; + +// Proxy logic for all routes under /api +router.all('/*', addSubscriptionKey, async (req, res) => { + try { + // Construct the APIMS URL + const targetUrl = `${process.env.APIMS_BASE_URL}${req.originalUrl}`; + + console.log(`[Proxy Middleware] Forwarding request to: ${targetUrl}`); + + // Make the request to APIMS + const response = await axios({ + method: req.method, + url: targetUrl, + headers: req.headers, // Forward all headers, including the subscription key + data: req.body, // Forward the request body + }); + + console.log(`[Proxy Middleware] Received response from APIMS: ${response.status}`); + // Send the response back to the client + res.status(response.status).send(response.data); + } catch (error) { + console.error('[Proxy Middleware] Error during request forwarding:', error.message); + + if (error.response) { + // If APIMS responded with an error, forward it to the client + console.error(`[Proxy Middleware] APIMS Response:`, error.response.data); + res.status(error.response.status).send(error.response.data); + } else { + // If no response from APIMS, send a generic error + res.status(500).send('Internal Server Error: Unable to process the request'); + } + } +}); + +module.exports = router; diff --git a/frontdoor-endpoints/routes.js b/frontdoor-endpoints/routes.js index e9fc783..51371e2 100644 --- a/frontdoor-endpoints/routes.js +++ b/frontdoor-endpoints/routes.js @@ -12,25 +12,16 @@ router.use((req, res, next) => { router.use('/employee', (req, res, next) => { console.log('[Debug: /employee Route] Request received:', req.originalUrl); next(); -}, apiProxy()); +}, apiProxy); router.use('/request', (req, res, next) => { console.log('[Debug: /request Route] Request received:', req.originalUrl); next(); -}, apiProxy()); +}, apiProxy); router.use('/liquidation-request', (req, res, next) => { console.log('[Debug: /liquidation-request Route] Request received:', req.originalUrl); next(); -}, apiProxy()); - - -router.use('/employee', apiProxy()); -router.use('/request', apiProxy()); -router.use('/liquidation-request', apiProxy()); -router.use('/vacations-info', apiProxy()); -router.use('/email_id', apiProxy()); -router.use('/employees-by-leader', apiProxy()); - +}, apiProxy); module.exports = router; diff --git a/index.js b/index.js index 0545238..77fe483 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ require('dotenv').config(); // routes const photoRoute = require('./routes/photoRoute'); -const apiFrontdoor = require('./frontdoor-endpoints/routes'); +const apiFrontdoor = require('./frontdoor-endpoints/frontdoor-api'); const employee_endpoint = require('./backdoor-endpoints/employee/routes'); const request_endpoint = require('./backdoor-endpoints/request/routes'); @@ -55,11 +55,8 @@ app.use((req, res, next) => { }); // Routes -app.use('/protected', apiBackdoor); // Attach protected API routes -app.use('/api', apiFrontdoor); // Attach additional API routes app.use('/employee-photos', photoRoute); - -// Add a log to confirm the mount point +app.use('/protected', apiBackdoor); // Attach protected API routes app.use('/api', (req, res, next) => { console.log('[Debug: Router Mount] /api prefix detected, passing to router'); next();