Last active
December 10, 2023 20:35
-
-
Save madkoding/442c3d0c8efb3be9d51fda92febaf5e7 to your computer and use it in GitHub Desktop.
Revisions
-
madkoding revised this gist
Dec 10, 2023 . 1 changed file with 58 additions and 48 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,60 +1,70 @@ // Import required modules const http = require("http"); const httpProxy = require("http-proxy"); // API target and proxy port configuration const API_TARGET = 'https://pokeapi.co/api/v2/'; const PROXY_PORT = 8080; // Create a proxy server const proxy = httpProxy.createProxyServer({}); // Function to send error response const sendError = (res, err) => { console.error('Proxy error:', err); // Log the error for debugging res.status(500).send({ error: err.toString(), message: "An error occurred in the proxy" }); }; // Proxy error handling proxy.on("error", (err, req, res) => { sendError(res, err); }); // Function to enable CORS const enableCors = (req, res) => { if (req.headers['access-control-request-method']) { res.setHeader('access-control-allow-methods', req.headers['access-control-request-method']); } if (req.headers['access-control-request-headers']) { res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']); } if (req.headers.origin) { res.setHeader('access-control-allow-origin', req.headers.origin); res.setHeader('access-control-allow-credentials', 'true'); } }; // Set header for CORS in proxy response proxy.on("proxyRes", (proxyRes, req, res) => { enableCors(req, res); }); // Create HTTP server and define request handling logic const server = http.createServer((req, res) => { // Handle OPTIONS method for CORS preflight if (req.method === 'OPTIONS') { enableCors(req, res); res.writeHead(200); res.end(); return; } // Proxy the request to the API proxy.web(req, res, { target: API_TARGET, secure: true, changeOrigin: true }, (err) => { sendError(res, err); }); }); // Start the server server.listen(PROXY_PORT, () => { console.log(`Proxy server listening on port ${PROXY_PORT}`); }); -
madKoding revised this gist
Jun 3, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ // Install http-proxy package first var http = require("http"), httpProxy = require("http-proxy"); var API_TARGET = 'https://pokeapi.co/api/v2/' -
madKoding renamed this gist
Jun 2, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
madKoding revised this gist
Jun 2, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ var http = require("http"), httpProxy = require("http-proxy"); var API_TARGET = 'https://pokeapi.co/api/v2/' var PROXY_PORT = 8080 // Create a proxy server with custom application logic var proxy = httpProxy.createProxyServer({}); @@ -57,4 +57,4 @@ var server = http.createServer(function(req, res) { }); }); server.listen(PROXY_PORT); -
madKoding created this gist
Jun 2, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ // Install node-http-proxy package first var http = require("http"), httpProxy = require("http-proxy"); var API_TARGET = 'https://pokeapi.co/api/v2/' var SERVER_PORT = 8080 // Create a proxy server with custom application logic var proxy = httpProxy.createProxyServer({}); var sendError = function(res, err) { return res.status(500).send({ error: err, message: "An error occured in the proxy" }); }; // error handling proxy.on("error", function (err, req, res) { sendError(res, err); }); var enableCors = function(req, res) { if (req.headers['access-control-request-method']) { res.setHeader('access-control-allow-methods', req.headers['access-control-request-method']); } if (req.headers['access-control-request-headers']) { res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']); } if (req.headers.origin) { res.setHeader('access-control-allow-origin', req.headers.origin); res.setHeader('access-control-allow-credentials', 'true'); } }; // set header for CORS proxy.on("proxyRes", function(proxyRes, req, res) { enableCors(req, res); }); var server = http.createServer(function(req, res) { // You can define here your custom logic to handle the request // and then proxy the request. if (req.method === 'OPTIONS') { enableCors(req, res); res.writeHead(200); res.end(); return; } proxy.web(req, res, { target: API_TARGET, secure: true, changeOrigin: true }, function(err) { sendError(res, err); }); }); server.listen(SERVER_PORT);