Skip to content

Instantly share code, notes, and snippets.

@madkoding
Last active December 10, 2023 20:35
Show Gist options
  • Select an option

  • Save madkoding/442c3d0c8efb3be9d51fda92febaf5e7 to your computer and use it in GitHub Desktop.

Select an option

Save madkoding/442c3d0c8efb3be9d51fda92febaf5e7 to your computer and use it in GitHub Desktop.

Revisions

  1. madkoding revised this gist Dec 10, 2023. 1 changed file with 58 additions and 48 deletions.
    106 changes: 58 additions & 48 deletions anticors-proxy-server.js
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,70 @@
    // Install http-proxy package first
    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({});
    var sendError = function(res, err) {
    return res.status(500).send({
    error: err,
    message: "An error occured in the proxy"
    });
    // 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"
    });
    };

    // error handling
    proxy.on("error", function (err, req, res) {
    sendError(res, err);
    // Proxy error handling
    proxy.on("error", (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']);
    }
    // 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['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');
    }
    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);
    // Set header for CORS in proxy response
    proxy.on("proxyRes", (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);
    });
    // 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);
    });
    });

    server.listen(PROXY_PORT);
    // Start the server
    server.listen(PROXY_PORT, () => {
    console.log(`Proxy server listening on port ${PROXY_PORT}`);
    });
  2. madKoding revised this gist Jun 3, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion anticors-proxy-server.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Install node-http-proxy package first
    // Install http-proxy package first
    var http = require("http"), httpProxy = require("http-proxy");

    var API_TARGET = 'https://pokeapi.co/api/v2/'
  3. madKoding renamed this gist Jun 2, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. madKoding revised this gist Jun 2, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.js
    Original 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 SERVER_PORT = 8080
    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(SERVER_PORT);
    server.listen(PROXY_PORT);
  5. madKoding created this gist Jun 2, 2021.
    60 changes: 60 additions & 0 deletions index.js
    Original 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);