admin管理员组文章数量:1389903
I have a node.js
/ express
project and I try to implement http-proxy-middleware
like this :
const app = express();
...
// 1st : `/mainUrls/*` are processed by the main app
app.use("/mainUrls", routerMainUrls);
....
// 2nd : `/api/url1` and `/api/url2` are forwarded to another app
const proxyTable: any = {
"/api/url1" : "http://localhost:3010",
"/api/url2" : "http://localhost:3020",
};
const options = {
router: proxyTable,
};
const myProxy = createProxyMiddleware(options);
app.use(myProxy);
...
// 3rd : All others URLs
app.use(routerAnotherUrls);
....
The proxy is working correctly. If I receive a url like /api/url1
it is forwarded to the right destination.
However, if the url does not match the first or second case, I want it to be handled by the 3rd. I have good reasons for anizing my routers this way.
How can I tell to http-proxy-middleware
to continue if the proxy table is not satisfied ?
I have a node.js
/ express
project and I try to implement http-proxy-middleware
like this :
const app = express();
...
// 1st : `/mainUrls/*` are processed by the main app
app.use("/mainUrls", routerMainUrls);
....
// 2nd : `/api/url1` and `/api/url2` are forwarded to another app
const proxyTable: any = {
"/api/url1" : "http://localhost:3010",
"/api/url2" : "http://localhost:3020",
};
const options = {
router: proxyTable,
};
const myProxy = createProxyMiddleware(options);
app.use(myProxy);
...
// 3rd : All others URLs
app.use(routerAnotherUrls);
....
The proxy is working correctly. If I receive a url like /api/url1
it is forwarded to the right destination.
However, if the url does not match the first or second case, I want it to be handled by the 3rd. I have good reasons for anizing my routers this way.
How can I tell to http-proxy-middleware
to continue if the proxy table is not satisfied ?
1 Answer
Reset to default 0Doesn't seem to be an option to call next middleware from router
, so, you might instead create a middleware that will check paths and either call proxy middleware, or another middleware:
const myProxy = createProxyMiddleware(options);
const proxyUrls = Object.keys(proxyTable);
app.use((req, res, next) => {
if (proxyUrls.includes(req.path)) {
myProxy(req, res);
} else {
routerAnotherUrls(req, res);
}
});
本文标签: nodejsHow to continue in case of the router is not satisfied using httpproxymiddlewareStack Overflow
版权声明:本文标题:node.js - How to continue in case of the router is not satisfied using http-proxy-middleware? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744651959a2617737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论