-
Expected behaviorchange the proxy target when an api is called Actual behaviorlog info display the new proxy is set up, but in fact it is still proxy to the old proxy target Setup
proxy middleware configurationvar opt = {
target: 'https://sitea.com/',
changeOrigin: true,
secure: false,
xfwd: true,
hostRewrite: true,
cookieDomainRewrite: true,
headers:'...'
}
app.use(proxyMiddleware('/api', opt));
router.get('/online', function (req, res) {
opt.target = 'http://siteb.com/';
app.use(proxyMiddleware('/api', opt));
console.log(chalk.red(`switching to site b proxy`));
res.json('ok');
}); |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Try the |
Beta Was this translation helpful? Give feedback.
-
@chimurai thanks. It works. |
Beta Was this translation helpful? Give feedback.
-
I'm using VueJS 2.x, how to change this proxy target dynamically ? Do you have some example to show ? |
Beta Was this translation helpful? Give feedback.
-
function customRouter(req) {
const { hostname } = req
const tenant = hostname.split('.')[0]
return `https://${tenant}.sub.domain.com`
}
module.exports = {
...
devServer: {
...
proxy: {
'^.*$': {
target: 'https://tenant.sub.domain.com',
router: customRouter
}
}
}
} An example for anybody else using Vue 2.x, I needed a dynamic target to pass-through a subdomain in a multi-tenant application (so when one of our customers runs |
Beta Was this translation helpful? Give feedback.
-
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
// let fullUrl = '/magic/_startOfTargethttps://_query2.finance.yahoo.com/v10/finance/quoteSummary/_endOfTarget_/'+symbol+'?modules=financialData';
app.use('/magic',
createProxyMiddleware({
target: 'http://localhost',
router: (req) =>{
return req.url.match(new RegExp('/_startOfTarget_' + "(.*)" + '/_endOfTarget_'))[1]; // protocol + host
},
logLevel: "debug",
changeOrigin: true,
pathRewrite: function (path, req) {
let p=path.match(new RegExp('/_endOfTarget_' + "(.*)" ))[1];
console.log('[http://DECADE.TW][customRouter][pathRewrite]',p);
return p;
},
})
);
}; Dynamic target by URL embedded target, ex yahoo finance API edit: code formatting |
Beta Was this translation helpful? Give feedback.
Try the
router
option: https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/router.md