我在 angular2 应用程序中有以下路由:
/** Application routes */
const routes: Routes = [
{ path: '', component: LandingComponent },
{ path: 'about', component: AboutUsComponent },
// catch all path, should go after all defined paths
{ path: '**', component: PageNotFoundComponent }
];
最近我不得不添加bs-config.js
以下内容:
var proxy = require('http-proxy-middleware');
// note: serving from both ./dist and ./node_modules
// TODO: https?
// redirect all /api calls to the backend
var apiProxy = proxy('/api', {
target: 'http://localhost:8080',
pathRewrite: {
'^/api' : '', // rewrite path
},
changeOrigin: true // for vhosted sites
});
module.exports = {
files : "./dist/**/*.{js, html, css}",
server: {
baseDir : ["./dist","node_modules"],
middleware: {
1: apiProxy
},
https : false
},
logLevel: "debug"
};
一切正常,除了访问 404 页面,即如果用户输入了错误的链接,我只会看到“无法获取 /url”并且日志中没有任何有趣的内容。如果我只删除这三行:
middleware: {
1: apiProxy
},
它再次开始工作,我在http://myapp/some/broken/url上获得 404 页面。
但我需要后端相关内容的代理。为什么它会干扰常规的 api 路径,即使它应该只代理 'api' 之类的 url?
PS我正在使用:
"http-proxy-middleware": "^0.17.2",
"lite-server": "^2.2.2",