【问题标题】:cors anywhere how to add https support?cors 任何地方如何添加 https 支持?
【发布时间】:2020-02-04 23:19:13
【问题描述】:

cors 如何添加 https 支持? 默认情况下只有 http 可用。

这是我安装并运行的脚本:

      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

【问题讨论】:

    标签: node.js


    【解决方案1】:

    这是我添加 https 支持的工作代码:

    我添加了 'dotenv' 以便能够使用 .env 文件,

    这是我的 .env 文件,它位于根目录。

    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
    

    https,需要private.key文件,和public.cert文件, 2 文件放在根目录下。

    您必须创建 2 台服务器,一台在端口 7000 上监听 http,另一台在端口 7200 上监听 https

    工作代码

     // 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);
       });
    

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      相关资源
      最近更新 更多