GithubHelp home page GithubHelp logo

chimurai / http-proxy-middleware Goto Github PK

View Code? Open in Web Editor NEW
10.5K 118.0 823.0 3.04 MB

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more

License: MIT License

JavaScript 0.95% TypeScript 99.05%
javascript proxy-middleware http-proxy proxy middleware nodejs browser-sync connect express websocket

http-proxy-middleware's People

Contributors

all-less avatar aremishevsky avatar bangank36 avatar bforbis avatar chimurai avatar cloudmu avatar crooruhe avatar davidmeirlevy avatar dependabot[bot] avatar dflock avatar dylang avatar flackenstein avatar graingert avatar kevinxh avatar klimashkin avatar leonardobazico avatar liranbri avatar lpaolini avatar maapteh avatar maple3142 avatar mcgwiz avatar mhassan1 avatar michaeltazelaar avatar midgleyc avatar oufeng avatar rsethc avatar sorin-davidoi avatar stefanwright1988 avatar tigge avatar trysound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

http-proxy-middleware's Issues

POST request body is not proxied to the servers

I have in my backend defined:

var proxy = proxyMiddleware('/api', {
        target: 'http://somehost.zero',
        proxyTable: {
            '/api/one': 'http://somehost.one',
            '/api/two': 'http://somehost.two',
        }
    });
app.use(proxy);

and when I'm proxing POST request (lets say to the '/api/one': 'http://somehost.one' ) I don't know why but server http://somehost.one get request without body.

Did you had similar problem?

Impossible to ignore proxy path prefix

Hi,

I need to proxy requests from /api to the root of other site: http://test.com:

/api/users -> http://test.com/users
/api/users/1 -> http://test.com/users/1

But all my requests lead to http://test.com/api/*
How to ignore /api prefix but not to ignore other path?
When I use ignorePath settings all my requests lead to http://test.com.

Config:

server.middleware = proxyMiddleware('/api', {target: 'http://test.com', proxyHost: 'test.com'});

Log levels

Introduce option.logLevel:

  • debug
  • info
  • warn
  • error
  • silent

Ability to control the log levels or disable logging.

Running subapps and applying proxy

Hi,

First of all thanks for the module, i tried it out and works fine. I'm looking for suggestions for my current use-case

I have 3 apps, a, b and c, where a is main app

a - main app running in port 80

  • route.js
    • app.get('/a',function(){})

b - sub app running in port 8001

  • route.js
    • app.get('/',function(){})

c - sub app running in port 8002

  • route.js
    • app.get('/',function(){})

In app a code look like this

   function onError(err, req, res) {
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('Something went wrong. And we are reporting a custom error message.');
    }

    var serverB = 'http://localhost:8001';
    var serverC = 'http://localhost:8002';

    var proxyB = proxyMiddleware('/b', {target: serverB, onError : onError});
    var proxyC = proxyMiddleware('/c', {target: serverC, onError : onError});
    app.use(proxyB );
    app.use(proxyC );

I will bring down b or c for deployment and will be showing maintenance page accordingly.

Like to know if this configuration is good enough to support my usecase.
Please share your thoughts

Change target at runtime

Hi! It would be great to have some method to change proxy target in runtime by some user interaction!
Without restarting express or httpserver

lost session with multiples hosts

I use multiple host on a proxy configuration. When I call several at the same time , I 'm losing session and the rest call always returns error 204 No content.

Any idea?

wildcard / glob support

Extend the current "context" matching, in order to have more control on which requests to proxy or not to proxy.

Proxy websocket when using gulp, browsersync, ng-websocket

Hi,

Can you please review the http-proxy-middleware config below?

I'm not able to proxy local websocket (separate) process with http-proxy-middleware as I see "[HPM] Upgrading to WebSocket" message on the window where I ran "gulp serve", and the error "WebSocket connection to 'ws://localhost:3000/ws/myobjects' failed: Connection closed before receiving a handshake response" on the browser console window.

I'm using v0.9.0 of http-proxy-middleware, v0.2.1 of ng-websocket, v2.8.3 of browser-sync.

When browser makes a websocket call to "ws://localhost:3000/ws/myobjects" I expect the call to be served by a server listening for web socket connection on "ws://localhost:10009/ws/myobjects"
There are other static assets served under http://localhost:3000 that are not related to websockets.

function browserSyncInit(baseDir, browser) {
  browser = browser === undefined ? 'default' : browser;

  var routes = null;
  if(baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
    routes = {
      '/bower_components': 'bower_components'
    };
  }

  var server = {
    baseDir: baseDir,
    routes: routes
  };

  /*
   * You can add a proxy to your backend by uncommenting the line bellow.
   * You just have to configure a context which will we redirected and the target url.
   * Example: $http.get('/users') requests will be automatically proxified.
   *
   * For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.0.5/README.md
   */
   server.middleware = proxyMiddleware('/ws/myobjects', {
     target: 'http://localhost:10009',
     ws: true,
     changeOrigin : true,
     proxyTable: {
       'localhost:3000/ws/myobjects' : 'http://localhost:10009'
     }
   });

  browserSync.instance = browserSync.init({
    startPath: '/',
    server: server,
    browser: browser
  });
}

Support shorthand proxy configuration

Support shorthand proxy configuration.

Set target = http://www.example.org and proxy everything:

var proxy = proxyMiddleware('http://www.example.org');
// equals:  proxyMiddleware('/', {target: 'http://www.example.org'});

Set target = http://www.example.org and context = '/api':

var proxy = proxyMiddleware('http://www.example.org/api');
// equals:  proxyMiddleware('/api', {target: 'http://www.example.org'});

Handling relative paths

Hi,

I am trying to use the proxy in the following setup :

//Hosted on http://localhost:4000
var proxyMiddleware = require('http-proxy-middleware');
var proxy = proxyMiddleware('/api/materials/Csi/Model', {
  target: 'http://localhost:8080/newApp',
  changeOrigin: true
});
app.use(proxy);

Now when I do - http://localhost:4000/api/materials/Csi/Model it does load the target but the dependencies pop out a 404 error.

I fixed for now using apache2 using mod_proxy (proxy reverse), but I want to use your npm module as can add route based authentication when on port 4000.

Thanks
Gautam

Edit proxy request/response POST parameters

Hello Chimurai,

I was hoping you might have some suggestions on how to edit the POST parameters prior to submitting the request to the target and redacting/removing sensitive parameters in the response prior to forwarding to the client? I can find a number of options to trap the body content using modules like getRawBody or using the req.on('data',..) to capture the form parameters but I can't find anything that would allow for the manipulation of the rawBody for both the request or response.

Hoping you might be able to shed some light on this problem?

Respectfully,

Warren.

expose http-proxy proxyRes event

ability to subscribe to http-proxy proxyRes event to inspect and modify the response.
example:

  • add/remove/modify response headers.

Custom log provider

Introduce option.logProvider

Ability to use a custom log provider, such as:

  • winston
  • log4js

Default logProvider uses console to do the logging.

option.logProvider = function (provider) {

   // override logger.error()
   provider.error = function (message) {
      // do something
   }

   return provider
}

Or complete replace the default log provider:

option.logProvider = function () {

   var provider = {
      log : console.log,
      debug : console.log,    // use .log(); console does not have .debug()
      info : console.info,
      warn : console.warn,
      error : console.error
   };

   return provider
}

Disable SSL cert checking

Hi is this possible ? I'm running against a dev server and can't get a valid cert until we go to prod.

Proxy error: ECONNREFUSED (Laravel)

Im trying to foward all requests from my Express app that runs on port 4365 to another application that runs on port 9000.
In my case im trying to build a Laravel app that will be used only for api porpuses and angular app that will use that api.

This is the code i use:

var app             = require('express')()
    , proxyMiddleware = require('http-proxy-middleware')
    , http          = require('http').Server(app);
var context = '/api'; 
var options = {
    target: 'http://localhost:9000', // target host
    changeOrigin: true,               // needed for virtual hosted sites
    ws: true,                         // proxy websockets
};
app.use(proxyMiddleware(context, options));
http.listen(4365, "localhost", function(){
    console.log('Express server is listening on Port 4365 ');
});

Assume i have Laravel app running on port 9000 with api routing valid.
When i run it and open the browser and type in the url: http:\localhost:4365\api\v1\comments i assume to get the json, but it shows me an error: "Error occured while trying to proxy to: localhost:4365/api/v1/comments"

console output:
[HPM] Proxy created: /api -> http://localhost:9000
Express server is listening on Port 4365
[HPM] Proxy error: ECONNREFUSED. localhost -> "localhost:9000/api/v1/comments"

Correctly proxy connect/express when proxy is mounted with the optional path

Support different ways of mounting:

app.use(proxy('/api', {target:'http://www.example.org'}));
app.use(proxy('http://www.example.org/api'));    // <-- shorthand 
app.use('/api', proxy('http://www.example.org'));   // <-- with optional path + shorthand

http://expressjs.com/4x/api.html#app.use
When optional path is configured, requests to /api/books/123 should be proxied to http://www.example.org/api/books/123

var proxy = require('http-proxy-middleware');

app.use('/api', proxy('http://www.example.org'));

How to configure tunneling proxy

Problem Description

I am using proxy middleware to communicate with an endpoint which lives in open internet but I am behind a corporate proxy. So when I try to connect to the endpoint I am getting following error.

ROXY ERROR: ENOTFOUND. localhost:3000 -> https://www.example.com.au/login

I wonder how to configure agent for http-proxy-middleware.

I have tested a simple https with a agent and works fine and code as follows.

var tunnel = require('tunnel');
var tunnelingAgent = tunnel.httpsOverHttp({
  proxy: {
    host: 'office.proxy.com.au',
    port: 8080
  }
})

var https = require('https')
  var post_req = https.request({
    host: 'www.example.com.au',
    port: '443',
    path: '/logon',
    method: 'GET',
    agent: tunnelingAgent,
    timeout: 10000,
    followRedirect: true,
    maxRedirects: 10
  }, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('Response: ' + chunk);
    });
  });
  post_req.write("name=mario");
  post_req.end();

[HPM] Proxy error: ECONNREFUSED

Hi, I am serving a web application with gulp.js in port 3000, and a php backend application served in port 8000.

I am trying to use http-proxy-middleware in my gulp serve task in order to defer any calls from localhost:3000/api to my api endpoint at: localhost:8000/api. I have used it as follows:

proxyMiddleware('/api', { target: 'http://localhost:8000' });

For instance if I try to POST to api/authenticate, nodejs gives me in the console:
[HPM] Proxy error: ECONNREFUSED localhost:8000/api/authenticate

So what I can see here is that it's actually trying to proxy it, but returns this unexpected error. Inspecting the network response I get:

Error occured while trying to proxy to: localhost:3000/api/authenticate

I don't know if this problem is related to myself using this module in an incorrect manner, any help would be very appreciated. Thanks in advance.

add proxyTable support

Suggested in #4 (comment)

proxyTable option allows you to use a different option.target based on host request header and optional path.

Proposal:

proxyMiddleware('/api', {
  target: 'http://www.example.org',
  proxyTable : {
    'foo.com/api': 'http://website.com:8000',
    'foo.com': 'http://website.com:8001',
    'bar.com': 'http://website2.com:8002'
  }
});

sockjs: WebSocketSession not yet initialized

I am having problems proxying SockJS websockets. On the server side I am getting "WebSocketSession not yet initialized". On the client side I am getting "failed: Invalid frame header". It looks like it almost connects, but something goes wrong in handshake. Any ideas?

Here's my proxy configuration.

var gulp            = require('gulp');
var express         = require('express');
var proxyMiddleware = require('http-proxy-middleware');

gulp.task('serve', function() {
    proxy = proxyMiddleware('/go', {
        target: 'http://localhost:8080/',
        ws: true,
    });

    var app = express();
    app.use('/', express.static('./build/'));             
    app.use(proxy);                                       

    var server = app.listen(3000);
    server.on('upgrade', proxy.upgrade);                  
});

gulp.task('build', function(){
    gulp.src('src/**/*.*')
        .pipe(gulp.dest('./build/go/'));
});

Network tab:
General

Request URL:ws://localhost:3000/go/xxxxxxxxxx/websocket
Request Method:GET
Status Code:101 Switching Protocols

Response Headers

view source
connection:upgrade
date:Sun, 20 Mar 2016 20:59:11 GMT
sec-websocket-accept:sPIVWZeoUMNdIxL4EHnYgvBoZng=
sec-websocket-extensions:permessage-deflate;client_max_window_bits=15
server:Apache-Coyote/1.1
upgrade:websocket

Websocket in Gulp(Ionic)

I copy your code in gulpfile.js

var proxy = require('http-proxy-middleware');

gulp.task('connect', function() {
     connect.server({
       root: ['./app'],
       port: 2301,
       middleware: function(connect, opt) {
         return [
           proxy('/socket', {
             target: 'https://cloud.vanwardsmart.com',
             changeOrigin:true,
             ws: true,      // <-- set it to 'true' to proxy WebSockets
             secure:true
           })
         ]
       }
     });
 });

gulp.task('default', ['connect']);

when I use create websocket instance, just like this:

  var wsUrl = 'wss://192.1.10.80:2301/ws';
  var ws = new WebSocket(wsUrl);

the browser show "failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED" error

x-www-form-urlencoded

Hi.
Header 'Content-Type: application/x-www-form-urlencoded'
Not working...
Error:
[HPM] Proxy error: ECONNRESET. localhost -> "api.*.com/oauth/token"

Proxy Failure on WS Proxy Server Connection Close

HTPM just got merged into webpack(webpack/webpack-dev-server#359) which is awesome because it enables things like native websockets which didn't work previously.

I was just taking it for a spin and everything was working great until I took down the backend server before shutting down the dev-server. The logic in my front-end is such that after the socket dies it will recheck if it is logged in (hence the /api/users/me check). I can work on an example but it may take me a little while, wondering if it might be obvious from these logs + stack trace:

[HPM] Upgrading to WebSocket
[HPM] Client disconnected
[HPM] Proxy error: ECONNREFUSED. 127.0.0.1 -> "127.0.0.1:9020/api/users/me"
[HPM] Upgrading to WebSocket
./node_modules/webpack-dev-server/node_modules/http-proxy-middleware/lib/handlers.js:8
        res.writeHead(500);
            ^

TypeError: res.writeHead is not a function
    at ProxyServer.proxyError (./node_modules/webpack-dev-server/node_modules/http-proxy-middleware/lib/handlers.js:8:13)
    at ProxyServer.emit (./node_modules/webpack-dev-server/node_modules/http-proxy-middleware/node_modules/http-proxy/node_modules/eventemitter3/index.js:117:27)
    at ClientRequest.onOutgoingError (./node_modules/webpack-dev-server/node_modules/http-proxy-middleware/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:153:16)
    at emitOne (events.js:77:13)
    at ClientRequest.emit (events.js:169:7)
    at Socket.socketErrorListener (_http_client.js:259:9)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1257:8)
    at doNTCallback2 (node.js:441:9)
    at process._tickCallback (node.js:355:17)

Im totally expecting the proxy to fail and even loudly complain, but I wouldn't expect it to crash the dev-server. If there's some hardening that must be done on the webpack-dev-server itself instead we can get a PR there.

Add support for redirection to corporate proxy

Hello,

I'm working behind a corporate proxy, and I can't seem to make redirection work through it.

Here is my config:

{
  context: '/api',
  options: {
    pathRewrite: { '^/api' : '' },
    target: 'http://api.stuff.com',
    changeOrigin: true,
    toProxy: 'http://x.x.x.x:8080/'
  }

Maybe it's a misuse of the toProxy option, but I can't find a good example usage.

In another hand, it would be nice to make use of the standard http_proxy and https_proxy environment variables to make this work automatically without having to fiddle through the config (most node modules use these to work through corporate proxies).

Thanks for your help

Proxy target supports local file path

I write some local mockup files to debug APIs when server-side is not ready.
It will be nice to write file path in target to proxy local file.

proxyMiddleware('/login/verify', {
    target: 'file:///home/someone/mockup',
    pathRewrite: {
      '^/login/verify': 'login-verify.json'
    }
})

Use proxy response cookie for further proxy request

Expected behavior

Not able to save cookies from proxy response to the session.

Actual behavior

Setup

  • http-proxy-middleware: 0.15.0
  • server: express + 4.13.3

proxy middleware configuration

const proxyOptions = {
  target: targetUrl,                // target host
  pathRewrite: function (path) {
    return path.replace('/api', '')
  },
  logLevel: 'debug',
  onProxyReq: function (proxyReq, req, rsp) {
    proxyReq.path = req.path + '?ticket=' + req.session.pt[targetUrl]
    if(req.session.cookie.pt_session_id && req.session.cookie.pt_session_id[targetUrl]) {
      proxyReq.writeHead(req.session.cookie.pt_session_id[targetUrl])
    }
  },
  onProxyRes: function onProxyRes(proxyRes, req, res) {
    console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2))
    req.session.cookie.pt_session_id = req.session.cookie.pt_session_id || {}
    req.session.cookie.pt_session_id[targetUrl] = proxyRes.headers['set-cookie']
  }
}
var apiProxy = proxy('/api', proxyOptions);

server mounting

var app = express();

app.use(apiProxy);
app.listen(3000);

Hi,

Thanks guys for the awesome middleware which made my life very easy. We need minor information regarding my usecase. We are implementing CAS proxy using then middleware. Our cas server behaviour is it, first time it accept the proxy ticket and provides the _session_id, it expects that _session_id in further request. I am not able to save it to session and use it. Have I missed something. ?

proxy response looks like below:

RAW Response from the target {
   "server": "nginx/1.6.2",
   "date": "Fri, 13 May 2016 06:38:26 GMT",
   "content-type": "application/json;charset=utf-8",
   "content-length": "2071",
   "connection": "close",
   "status": "200 OK",
   "set-cookie": [
     "_session_id=randombignumber; path=/; HttpOnly"
   ]
}

proxy to https errors in Node 0.12

Hi,

When proxying to https, ie:

var myProxy = httpProxyMiddleware('/my-path', {target: 'https://mysslexample.com'});

I first get a 'Proxy created' message, but then when I try to go to /my-path, I get the following error:

[HPM] Proxy error: undefined mysslexample.com/my-path

and nothing works. It works in Node 0.10.33, but not in Node 0.12. Is this an http-proxy-middleware issue or an http-proxy issue?

Thanks!

Expose `proxyReqWs` events

We can add a onProxyReqWs callback analogous to onProxyReq for the proxyReq events.

Alternatively, we could expose the proxy object itself. That'd probably give the user the most flexibility in attaching callbacks.

Api behind Basic authentication and NTLM authentication

Hi, I have api on iis server behind basic windows authentication and i cannot use cors. So I tried to use this module but however I configure it I cannot log into api and I get 401 every time

I tried

    server.middleware = proxyMiddleware(
        '/api',
        {
            target: 'API_HOST',
            logLevel: 'debug'
        }
    );
    server.middleware = proxyMiddleware(
        '/api',
        {
            target: 'API_HOST',
            logLevel: 'debug',
            auth: 'LOGIN:PASS'
        }
    );
    server.middleware = proxyMiddleware(
        '/api',
        {
            target: 'http://LOGIN:PASS@API_HOST',
            logLevel: 'debug'
        }
    );

TypeError: Cannot read property 'originalUrl' of undefined

Expected behavior

No Error

Actual behavior

Error

HPM] Proxy created: /  ->  http://localhost/
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]
/ .. ...  .. ..... /node_modules/http-proxy-middleware/index.js:38
        req.url = req.originalUrl;
                     ^
TypeError: Cannot read property 'originalUrl' of undefined
    at middleware (/media/stefano/Windows8_OS/Tinvention/tinvention/tinvention/consulenze/Unicredit/ExCeed/configDB/30_sviluppo/config_db_client/trunk/node_modules/http-proxy-middleware/index.js:38:22)

Setup

  • http-proxy-middleware: version -> "http-proxy-middleware": "0.15.0",
  • server: connect/express/browser-sync... + version -> "browser-sync": "^2.10.0",

proxy middleware configuration

var proxyMiddleware = require("http-proxy-middleware");


var proxyTable = {
    "/exx" : "http://localhost:5000/exx",    // host only
    "/configclient"     : "http://localhost:3000"    // host only
};

var options = {
    target: 'http://localhost/',
    proxyTable: proxyTable,
    logLevel: 'debug',
    changeOrigin: false
};

var proxyMidd = proxyMiddleware(options);

server mounting

browser-sync start -c ./bs-config.js

bs-config.js:

module.exports = {
    "ui": {
        "port": 3001,
        "weinre": {
            "port": 8080
        }
    },
    "files": false,
    "watchOptions": {},
    //"server": false,
    "server": {
        baseDir: "./"
    } ,
    "proxy": [ proxyMidd() ],
    "port": 3000,
    "middleware": [ historyApiFallback() ],
   ...

Confusing docs around path rewrite

The documentation around the path rewrite config object is a bit confusing. One could infer that the object matches as many times it can against the path when in reality it stops as soon as it hits the first match.

https://github.com/chimurai/http-proxy-middleware/blob/master/lib/path-rewriter.js#L28

I am also not sure if _.forEach is the best choice here either as it further drives home the idea of cycling through everything.

Plus it seems like there could be use cases for looping through a set of simple rewrites against one path instead of overly complex keys to match in one go.

There is also no mention of priority, and how can priority even be guaranteed since you are using _.forIn on object keys.

EventSource not working

I don't know if this is a http-proxy-middleware problem or a browser-sync problem (reported there as well BrowserSync/browser-sync#872) but I can't get EventSource to work when using this combination.

I am trying to use EventSource from my node-js app and push events to the client -

Has anyone managed to get this working ? I have no errors, no messages on the console but also no events. :(

If I run an example app just using node, things work fine so I know it's a problem with the combination of browser-sync and http-proxy-middleware

thanks

custom proxy error handler

add ability to override the default http-proxy error handling:

  • to return a different http status code.
  • to return a different response.
  • to return nothing.
  • etc...

pathRewriter happening before ProxyTable

Hi,

My config are like the following one:

(pathRewrite)
{ '^/api/one' : '', '^/api/two' : '' }

and

(proxyTable)
{ '/one' : 'http://localhost:3001', '/two' : 'http://localhost:3002' }

My use case is that I need the complete path to choose the correct proxy, and only after that it should apply my rules concerning pathRewrite.

If I put the pathRewrite step after the proxyTable one, it works well.

Have I missed something?
What would be a solution?

websocket problems (browser-sync + http-proxy-middleware)

so, I'm trying to proxy a websocket within gulp, using http-proxy-middleware.

my backend server code is this

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server,{ path: '/socket.io'});
io.on('connection', function(){ console.log("woot! connection")/* … */ });
server.listen(5000);

and my gulp code is this

  var server = {
    baseDir: baseDir,
    routes: routes
  };

  var proxies = [];

  proxies.push(proxyMiddleware('/socket.io', { target: 'http://localhost:5000/' , ws: true}));
  server.middleware = proxies;

  browserSync.instance = browserSync.init({
    startPath: '/',
    server: server,
    browser: browser
  });

on the console I see

woot! connection
[HPM] Upgrading to WebSocket
[HPM] Client disconnected

and the client console has

WebSocket connection to 'ws://mysite.io/socket.io/?EIO=3&transport=websocket&sid=5_BeDfck0LFYcYxPAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502

Am I missing something ?

thanks

feature: add `router` option to dynamically route to different `target`

Allow user to provide a function to (re)route to a different target; context information from the request object can be used to determine the target as user wishes.

var apiProxy = proxy('/api', {
  target: 'http://localhost:1234',
  router: function (req) {
    return 'http://newtarget.localhost:5000';
    // return;  // to use default `target`
  }
});

Deprecate proxyTable in favor of router. The proxyTable functionality can still be used by providing a map (proxyTable) with routing rules.

var proxyTable = {
    "integration.localhost:3000" : "http://localhost:8001",    // host only
    "staging.localhost:3000"     : "http://localhost:8002",    // host only
    "localhost:3000/api"         : "http://localhost:8003",    // host + path
    "/rest"                      : "http://localhost:8004"     // path only
};

var apiProxy = proxy('/api', {
  target: 'http://localhost:1234',
  router: proxyTable
});

References:

How can I proxy only GET method

I would like to proxy only some type of methods (in case I have REST api on same url with GET and POST method). Server has implemented only POST method and I should test only that. I can't specify just to proxy POST.

Is it possible to add that feature?

Client certificate isn't been recognized

Hi,

I'm creating a proxy to a server that uses certificate authentication.

    server.middleware = proxyMiddleware('/providers/sse/services/scim',
        {target: 'https://backendserver.com',
          changeOrigin: true,
          port: 443,
          secure: true,  // ignore cert errors
          agent: false,
          https: {
            key: fs.readFileSync('private_key.pem'),
            cert: fs.readFileSync('client_cert.pem')
          },
        });

However, when I connect to my server, I'm being prompted for the credentials when the request is being forwarded to the backend server.

At the same time, the following code (written by someone else) that creates an http server directly, passing the same client certificate data, passes the client cert data successfully, and I can retrieve data from the backend server without a prompt:

http.createServer(onRequest).listen(3030);

function onRequest(client_req, client_res) {

  var options = {
    key: fs.readFileSync('private_key.pem'),
    cert: fs.readFileSync('client_cert.pem'),
    agent: false,
    host: backendserver,
    method: client_req.method,
    path: client_req.url,
    headers:{
        Host: backendserver
    }
  };

  var proxy = https.get(options, function(res) {
    client_res.on('data', function(d) {
  });

  res.pipe(client_res, {
      end: true
    });
  });

  client_req.pipe(proxy, {
    end: true
  });

Any idea of what might be wrong?

Allow to subscribe to websocket http upgrade externally

Listen to upgrade event externally, without hitchhiking on a initial http request.

var app = express();
var proxy = proxyMiddleware('http://www.example.org/api', {ws:true, changeOrigin: true});

app.use(proxy);

var server = app.listen(3000);

server.on('upgrade', proxy.upgrade);  // <-- directly subscribe to server's upgrade event.

How to log request and response urls?

I would like to log each request no matter what status code the target server responded. I'm trying to achieve the following format:

// Format:
// -->  [METHOD] [PATH_REQUESTED_FROM_PROXY] -> [URL_REQUESTED_FROM_TARGET]
// Example:
// -->  GET /v1/users/123 -> https://my-app.herokuapp.com/api/v1/users/123

My proxy options are:

var proxyOpts = {
    target: 'https://my-app.herokuapp.com/',
    pathRewrite: {
        '^/v1' : '/api/v1'
    },
    onProxyReq: function onProxyReq(proxyReq, req, res) {
        // Log outbound request to remote target
        console.log('-->  ', req.method, req.path, '->', proxyReq.baseUrl + proxyReq.path);
    },
    onError: function onError(err, req, res) {
        console.error(err);
        res.status(500);
        res.json({error: 'Error when connecting to remote server.'});
    },
    logLevel: 'debug',
    changeOrigin: true,
    secure: true
};

Which outputs:

-->   GET /api/v1/users/123 -> undefined/api/v1/users/123

Problems:

  • req.path is already converted to the new format with pathRewrite, I would want to log the url which client actually requested from this proxy server
  • proxyReq.baseUrl is not defined, I'm not sure what I should use to get the host part of the request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.