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;
}
}