章節連結
pathRewrite 在 http-proxy-middleware 這個套件,除了可以用物件 key-value 來覆寫路由路徑,也可給一個 Function 來自訂。這邊筆記下自定義 pathRewrite 的方式。
內容
這回的情境是絕大多數都是導向 a 位置,但少數幾隻要導向 b 位置。那麼可以這樣寫:
|
1 2 3 4 5 6 7 8 9 |
{ pathRewrite: (path, req) => { if (req.path.includes('language_request')) { return path.replace(req.path, '/language_request') } return path } } |

![[筆記] Node.js http-proxy-middleware 部分更改路由 pathRewrite [筆記] Node.js http-proxy-middleware 部分更改路由 pathRewrite](https://smlpoints.com/wp-content/uploads/guide-npm-change-default-download-resources-in-china-1.png)