0

Is it possible to perform the following route/path-rewrite using http-proxy-middleware?

'/sec/port/xxx/yyy' => target:'https://someotherSite.com:port/xxx/yyy'

where the port is dynamic depending on the initial address i.e.

/sec/1234/xxx/yyy => https://someotherSite.com:1234/xxx/yyy

I'm using an express server.

1 Answers1

0

You'll want to rewrite the path so that the /api/PORT is removed, this can be done within the pathRewrite function. newPort is the port value which is obtained from the second index of req's original url split.

The router obtains the port number required from the request parameter and simply concatenates it as the return value, dynamically changing the target to localhost:PORTNUM.

Hope this helps.

proxyTable: {
'/api': {
    target: 'http://localhost',
    changeOrigin: true,
    pathRewrite:
        function(path,req) {
            //Get the port number
            var newPort = req.originalUrl.split('/')[2];
            //Return the path with the api and portname removed
            return path.replace('/api/'+newPort,'');
        },
    router: function(req) {
        var newPort = req.originalUrl.split('/')[2];
        //Dynamically update the port number to the target in router
        return 'http://localhost:'+newPort;
    }
}