I have an express-http-proxy used to redirect the request to another server. 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?
https://github.com/villadora/express-http-proxy
filter (supports Promises) The filter option can be used to limit what requests are proxied. Return true to continue to execute proxy; return false-y to skip proxy for this request.
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, ''));
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.