javascript 节点脚本将所有http请求转发到另一个服务器,并使用access-control-allow-origin标头返回响应。关注redi

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 节点脚本将所有http请求转发到另一个服务器,并使用access-control-allow-origin标头返回响应。关注redi相关的知识,希望对你有一定的参考价值。

// Simple proxy/forwarding server for when you don't want to have to add CORS during development.

// Usage: node proxy.js
//    Open browser and navigate to http://localhost:9100/[url]
//      Example: http://localhost:9100/http://www.google.com

// This is *NOT* for anything outside local development. It has zero error handling among other glaring problems.

// This started as code I grabbed from this SO question: http://stackoverflow.com/a/13472952/670023

var url = require('url')
  , http = require('http')
  , https = require('https');

var PORT = process.argv[2] || 9100;

var server = http.createServer(function(req, res) {
  var reqUrl = req.url.substr(1);
  console.log('==> Making req for' + reqUrl + '\n');

  req.pause();

  var options = url.parse(reqUrl);
  options.headers = req.headers;
  options.method = req.method;
  options.agent = false;

  options.headers['host'] = options.host;

  var connector = (options.protocol == 'https:' ? https : http).request(options, function(serverResponse) {
    console.log('<== Received res for', serverResponse.statusCode, reqUrl);
    console.log('\t-> Request Headers: ', options);
    console.log(' ');
    console.log('\t-> Response Headers: ', serverResponse.headers);

    serverResponse.pause();

    serverResponse.headers['access-control-allow-origin'] = '*';

    switch (serverResponse.statusCode) {
      // pass through.  we're not too smart here...
      case 200: case 201: case 202: case 203: case 204: case 205: case 206:
      case 304:
      case 400: case 401: case 402: case 403: case 404: case 405:
      case 406: case 407: case 408: case 409: case 410: case 411:
      case 412: case 413: case 414: case 415: case 416: case 417: case 418:
        res.writeHeader(serverResponse.statusCode, serverResponse.headers);
        serverResponse.pipe(res, {end:true});
        serverResponse.resume();
      break;

      // fix host and pass through.  
      case 301:
      case 302:
      case 303:
        serverResponse.statusCode = 303;
        serverResponse.headers['location'] = 'http://localhost:'+PORT+'/'+serverResponse.headers['location'];
        console.log('\t-> Redirecting to ', serverResponse.headers['location']);
        res.writeHeader(serverResponse.statusCode, serverResponse.headers);
        serverResponse.pipe(res, {end:true});
        serverResponse.resume();
      break;

      // error everything else
      default:
        var stringifiedHeaders = JSON.stringify(serverResponse.headers, null, 4);
        serverResponse.resume();
        res.writeHeader(500, {
          'content-type': 'text/plain'
        });
        res.end(process.argv.join(' ') + ':\n\nError ' + serverResponse.statusCode + '\n' + stringifiedHeaders);
      break;
    }

    console.log('\n\n');
  });
  req.pipe(connector, {end:true});
  req.resume();
});

console.log('Listening on http://localhost:%s...', PORT);
server.listen(PORT);

以上是关于javascript 节点脚本将所有http请求转发到另一个服务器,并使用access-control-allow-origin标头返回响应。关注redi的主要内容,如果未能解决你的问题,请参考以下文章

wrk中的lua脚本(转)

ElasticSearch架构思考(转)

前端优化

从 iOS UIAutomation 发出同步 HTTP GET 请求或在 JavaScript 中调用 shell 脚本

如何将多个外部 Javascript 脚本合并为一个?

(转) 如何在JavaScript与ActiveX之间传递数据1