0

我在angular2應用以下路由:瀏覽器的同步和精簡版服務器:HTTP代理中間件攔截不存在的網址

/** 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頁面,即如果用戶鍵入錯誤的鏈接,我只看到「無法GET/URL」和沒有什麼有趣的日誌。如果我只是刪除這些三條線:

 middleware: { 
      1: apiProxy 
     }, 

它再次開始工作,我得到http://myapp/some/broken/url 404頁。

但我需要代理的後端相關的東西。爲什麼它會干擾常規的api路徑,即使它只需要像url一樣代理'api'?

P.s.我使用:

"http-proxy-middleware": "^0.17.2", 
"lite-server": "^2.2.2", 

回答

0

通過改變以某種方式固定它:

middleware: { 
     1: apiProxy, 
     2: require('connect-history-api-fallback')({index: '/index.html', verbose: false}) 
    }, 
相關問題