如何解决调试 createProxyMiddleware 路径重写
很高兴知道我具体做错了什么,但除此之外,我很想知道如何打印出真正的路径重写实际上是什么。我尝试设置 DEBUG=express-http-proxy,但没有得到任何帮助。貌似proxy-forwarding 有很强的声明式语法,但是我还没有找到可以打印出最终结果的方法。
我的 Node/Express/Middleware 服务器正在尝试将所有以 /spreadsheet-upload/* 开头的请求转发到角度服务器,前提是调用方以 /spreadsheet-upload 开头。例如,如果有人将浏览器指向 https://localhost/spreadsheet-upload/page-1,Angular 服务器应该为 page-1 提供服务。基本上去掉 /spreadsheet-upload 部分,然后将其发送到主机名为 config.spreadsheet_client_host 的服务器。
我得到一个 200 返回码,但我得到一个空白页,并且浏览器的控制台中出现此错误:
Angular is running in the development mode. Call enableProdMode() to enable the production mode. vendor.js:41938:17
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'spreadsheet-upload'
./node_modules/@angular/router/fesm5/router.js/ApplyRedirects.prototype.noMatchError@https://localhost:8080/vendor.js:68322:16
./node_modules/@angular/router/fesm5/router.js/ApplyRedirects.prototype.apply/<@https://localhost:8080/vendor.js:68303:29
./node_modules/rxjs/_esm5/internal/operators/catchError.js/CatchSubscriber.prototype.error@https://localhost:8080/vendor.js:82540:31
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._error@https://localhost:8080/vendor.js:77522:26
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.error@https://localhost:8080/vendor.js:77496:18
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._error@https://localhost:8080/vendor.js:77522:26
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.error@https://localhost:8080/vendor.js:77496:18
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscr…
resolvePromise https://localhost:8080/polyfills.js:3170
resolvePromise https://localhost:8080/polyfills.js:3127
scheduleResolveOrReject https://localhost:8080/polyfills.js:3231
invokeTask https://localhost:8080/polyfills.js:2762
onInvokeTask https://localhost:8080/vendor.js:42628
invokeTask https://localhost:8080/polyfills.js:2761
runTask https://localhost:8080/polyfills.js:2534
drainMicroTaskQueue https://localhost:8080/polyfills.js:2940
转发片段是:
app.use(['/spreadsheet-upload','/spreadsheet-upload/*'],ensureAuthenticated,function (req,res,next) {
logger.debug('Route for spreadsheet-upload originalUrl=' + req.originalUrl + ': Forwarding to ' + config.spreadsheet_client_host);
next();
},createProxyMiddleware({
target: config.spreadsheet_client_host,changeOrigin: true,limit: "4mb",memoizeHost: false,preserveHostHdr: true,https: false,secure: false,proxyErrorHandler: function (err,next) {
util.inspect(err);
next();
},logLevel: 'debug',onProxyReq: function (proxyReq,req,rsp) {
Object.keys(req.headers).forEach(function (key) {
proxyReq.setHeader(key,req.headers[key]);
});
var accesstoken = generatetoken(req.user);
/**
* Set bearer token based on code from Sprint-API's lua code
*/
proxyReq.setHeader('Authorization','Bearer ' + accesstoken);
},onProxyRes: function onProxyRes(proxyRes,res) {
Object.keys(proxyRes.headers).forEach(function (key) {
res.append(key,proxyRes.headers[key]);
});
},pathRewrite: {
'^\/spreadsheet-upload\/': '/'
}
})
);
我曾经有过
router: {
'.*:8080': config.spreadsheet_client_host
}
在 createProxyMiddleware 中,但在 stackoverflow 上读到它不受支持。
这个片段确实有效:
app.use('/sprint-cost-recovery*',next) {
logger.debug('Route: ' + req.originalUrl + ': Forwarding to ' + config.sprint_cost_recovery_client_host);
next();
},forward_proxy(config.sprint_cost_recovery_client_host,{
limit: '2mb',proxyReqPathResolver: req => {
logger.debug('Route: /sprint-cost-recovery: proxyReqPathResolver() function reached');
util.inspect(req.originalUrl);
util.inspect(req.hostname);
util.inspect(req.https_port);
const result = url.parse(req.originalUrl).path;
logger.debug('Route: /sprint-cost-recovery: proxyReqPathResolver(): result=' + result);
return result;
}
})
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。