【问题标题】:encodeURIComponent on dynamic request url, node.js动态请求 url 上的 encodeURIComponent,node.js
【发布时间】:2019-01-27 11:48:04
【问题描述】:

我正在请求一个 index.djvu,它通过 id 触发对文档的请求。 index.djvu 触发的请求 id 是 Base64 编码的,例如 document/page_1_1_1_k21g_MjE4MGstMTAvMTE=.djvu,其中 id 是 MjE4MGstMTAvMTE=。 我需要在发送请求之前对 id 进行编码,例如 encodeURIComponent('MjE4MGstMTAvMTE=')。 如何控制 index.djvu 触发的请求?

我的代码是这样的

var http = require('http');
var httpProxy = require('http-proxy');
var HttpProxyRules = require('http-proxy-rules');

var proxyRules = new HttpProxyRules({
  rules: {
    // This requests response index further requests explained
    '.*/document': 'https://api.service.xxx/document/index.djvu?archive=21&id=2180k-10/11'
  },
  default: 'https://api.service.xxx/document'
});

var proxy = httpProxy.createProxyServer({
  secure: false
});

proxy.on('proxyReq', function (proxyReq, req, res, options) {
  proxyReq.setHeader('Authorization', 'Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');

});

var server = {};
server.httpServer = http.createServer(function (req, res) {
  var target = proxyRules.match(req);
  if (target) {
    return proxy.web(req, res, {
      target: target
    });
  }
});

server.init = () => {
  server.httpServer.listen(9000, () => {
    console.log('\x1b[36m%s\x1b[0m', 'The HTTP server is running on port 9000');
  });
}
server.init();

server.httpServer.on('error', function (err) {
  console.log('Error http-server\n', JSON.stringify(err));
});

proxy.on('proxyRes', function (proxyRes, req, res) {
  console.log('Status: ' + proxyRes.statusCode + ' ' + proxyRes.statusMessage);
  proxyRes.on('data', function (chunk) {
    console.log('Body: ' + chunk.toString('utf8'));
  });
  console.log('Headers received from api: ', JSON.stringify(proxyRes.headers, true, 2));
});

感谢所有帮助

【问题讨论】:

    标签: node.js url request encode node-http-proxy


    【解决方案1】:

    这在经过几个小时的战争后解决了;)

    var http = require('http');
    var httpProxy = require('http-proxy');
    var url = require('url');
    
    var proxy = httpProxy.createProxyServer({
      target: 'https://api.xxxxxxxxxxxxxx.xxx',
      secure: false
    }).listen(9000);
    
    proxy.on('proxyReq', function (proxyReq, req, res, options) {
      proxyReq.setHeader('Authorization', 'Bearer xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx');
      var url_parts = url.parse(req.url);
      var str = url_parts.pathname;
      var query = url_parts.search;
      if (str) {
        var path = str.split("_");
        urlstring = {
          page_: path[0],
          vers: path[1],
          subdoc: path[2],
          page: path[3],
          archive: path[4],
          enc_id: path[5]
        }
      }
      var rewritedPath;
      if (str.includes('route')) {
        rewritedPath = '/xxx' + str;
      } else if (str.includes('route')) {
        var index = '/xxx' + str + query;
        rewritedPath = index;
      } else {
        var page = 'xxx' + urlstring.page_ + '_' + urlstring.vers + '_' + urlstring.subdoc + '_' + urlstring.page + '_' + urlstring.archive + '_' + encodeURIComponent(urlstring.enc_id);
        rewritedPath = page;
      }
      proxyReq.path = rewritedPath;
    });
    
    proxy.on('error', function (err, req, res) {
      console.log('Something went wrong.', err);
    });
    
    proxy.on('proxyRes', function (proxyRes, req, res) {
      // console.log('Status: ' + req.url + ' ' + proxyRes.statusMessage);
      // proxyRes.on('data', function (chunk) {
      //   console.log('Body: ' + chunk.toString('utf8'));
      // });
      // console.log('Headers: ', JSON.stringify(proxyRes.headers, true, 2));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多