简体   繁体   English

从一些请求中绕过nodejs express-http-proxy

[英]bypass nodejs express-http-proxy from some requests

I have an express-http-proxy used to redirect the request to another server. 我有一个express-http-proxy用于将请求重定向到另一台服务器。 I would like to redirect some requests only, here below are the redirected requests code; 我只想重定向一些请求,下面是重定向的请求代码;

app.use('/api', proxy('abc.com', {
// preserveHostHdr: true,
https: isHttps,
proxyReqPathResolver: function (req) {
    return require('url').parse(req.url).path;
}}));

I tried 我试过了

app.use('/', proxy('abc.com', {
// preserveHostHdr: true,
https: isHttps,
proxyReqPathResolver: function (req) {
    return require('url').parse(req.url).path;
}}));

after the first block to bypass any other requests but I found out that proxy keeps forwarding any request. 在第一个块之后绕过任何其他请求,但我发现代理不断转发任何请求。 is there a way to bypass the proxy and call some HTML files on the same host? 有没有一种方法可以绕过代理并在同一主机上调用一些HTML文件?

https://github.com/villadora/express-http-proxy https://github.com/villadora/express-http-proxy

filter (supports Promises) The filter option can be used to limit what requests are proxied. filter(支持Promises)filter选项可用于限制代理哪些请求。 Return true to continue to execute proxy; 返回true继续执行代理; return false-y to skip proxy for this request. 返回false-y以跳过此请求的代理。

app.use('/proxy', proxy('www.google.com', {
  filter: function(req, res) {
     return req.url === 'http://www.urltobeproxied.com';
  }
}));

also you can intercept to return data of your choice 您也可以拦截以返回您选择的数据

app.use('/proxy', proxy('www.google.com', {
  userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
    data = JSON.parse(proxyResData.toString('utf8'));
    data.newProperty = 'exciting data';
    return JSON.stringify(data);
  }
}));

I found the solution as bellow; 我发现解决方法如下。 I must first declare a static path to express and all of the path content will be public. 我必须首先声明一个静态路径来表示,并且所有路径内容都将是公共的。 after that ı did the redirection. 之后,ı进行了重定向。

app.use('/api', proxy('abc.com', {
        // preserveHostHdr: true,
        https: isHttps,
            proxyReqPathResolver: function (req) {
            return require('url').parse(req.url).path;
        }
    }));

app.use(express.static("./web"));
app.get('/',function(req,res){
         res.sendFile(require('path').join(__dirname, ''));
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM