-2

cors anywhere how to add https support? By default only http available.

here is script I install and run it:

      heroku 
      https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe

     git clone https://github.com/Rob--W/cors-anywhere.git
     cd cors-anywhere/
     npm install
     heroku create
     git push heroku master


     windows:    
     https://stackoverflow.com/questions/18978751/how-to-run-node-js-app-from-cmd-with-some-predefined-port-in-windows

    npm install
    set PORT=7777
    set CORSANYWHERE_WHITELIST=https://transparentgov.net,http://transparentgov.net,http://transparentgov.net:3000,https://transparentgov.net:3200,https://transparentgov.net:10,http://transparentgov.net:10
    node server.js



    centos:

    npm install
    export PORT=7777
    export CORSANYWHERE_WHITELIST=https://transparentgov.net,http://transparentgov.net,http://transparentgov.net:3000,https://transparentgov.net:3200,https://transparentgov.net:10,http://transparentgov.net:10      
    node server.js
hoogw
  • 4,982
  • 1
  • 37
  • 33

1 Answers1

1

This is my working code for add https support :

I add 'dotenv' to be able to use .env file,

this is my .env file, it is at root directory.

PORT=7000
PORT_https=7200 
CORSANYWHERE_WHITELIST=https://transparentgov.net,http://transparentgov.net,http://transparentgov.net:3000,https://transparentgov.net:3200,https://transparentgov.net:10,http://transparentgov.net:10,http://localhost:10,http://localhost:3000,https://localhost:3200

the https, need private.key file, and public.cert file, 2 file put in root directory.

you must create 2 server, one listening on port 7000 for http, another server listening on port 7200 for https

working code

 // must install load dotenv to be able to use .env file
 require('dotenv').config();

 var fs = require('fs');

 // Listen on a specific host via the HOST environment variable
 var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 8080;  // for http



   var port_https = process.env.PORT_https || 8080; // for https




  // Grab the blacklist from the command-line so that we can update the blacklist without deploying
   // again. CORS Anywhere is open by design, and this blacklist is not used, except for countering
     // immediate abuse (e.g. denial of service). If you want to block all origins except for some,
  // use originWhitelist instead.
   var originBlacklist = parseEnvList(process.env.CORSANYWHERE_BLACKLIST);
    var originWhitelist = parseEnvList(process.env.CORSANYWHERE_WHITELIST);
     function parseEnvList(env) {
      if (!env) {
          return [];
               }
       return env.split(',');
       }



          console.log('port, http, https : ', port, port_https)
          console.log('originWhitelist', originWhitelist)






      // Set up rate-limiting to avoid abuse of the public CORS Anywhere server.
        var checkRateLimit = require('./lib/rate-limit')(process.env.CORSANYWHERE_RATELIMIT);



     // ************** http ***************************


    var cors_proxy = require('./lib/cors-anywhere');


    cors_proxy.createServer({

      originBlacklist: originBlacklist,
      originWhitelist: originWhitelist,
      requireHeader: ['origin', 'x-requested-with'],
      checkRateLimit: checkRateLimit,
      removeHeaders: [
        'cookie',
        'cookie2',
        // Strip Heroku-specific headers
        'x-heroku-queue-wait-time',
        'x-heroku-queue-depth',
        'x-heroku-dynos-in-use',
        'x-request-start',
      ],
      redirectSameOrigin: true,
      httpProxyOptions: {
        // Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
        xfwd: false,
      },
    }).listen(port, host, function() {
      console.log('Running CORS Anywhere http :  ' + host + ':' + port);
    });


     // ************** end *************  http ***************************





     // ************** https ***************************
    var cors_proxy_https = require('./lib/cors-anywhere');


    cors_proxy_https.createServer({

    // add https support  
    //https://github.com/Rob--W/cors-anywhere/issues/74



      httpsOptions: {
        key: fs.readFileSync(__dirname + '/private.key', 'utf8'),
        cert: fs.readFileSync(__dirname + '/public.cert', 'utf8')
      },


     // ********** end **** https ***************************

   originBlacklist: originBlacklist,
   originWhitelist: originWhitelist,
   requireHeader: ['origin', 'x-requested-with'],
   checkRateLimit: checkRateLimit,
    removeHeaders: [
'cookie',
'cookie2',
// Strip Heroku-specific headers
'x-heroku-queue-wait-time',
'x-heroku-queue-depth',
'x-heroku-dynos-in-use',
'x-request-start',
],
     redirectSameOrigin: true,
     httpProxyOptions: {
     // Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
         xfwd: false,
     },
    }).listen(port_https, host, function() {
     console.log('Running CORS Anywhere https ' + host + ':' + port_https);
   });
hoogw
  • 4,982
  • 1
  • 37
  • 33