app.use("/", (req, res) => {
res.redirect("https://www.google.com");
});
app.use("/app/v1", app);
我有此代碼并嘗試僅在localhost:9999/. 但是,當我這樣做時localhost:9999/app/v1,我仍然會被應用程式重定向。無論如何,我可以將 express 設定為僅在傳入的 url 為 時重定向/嗎?
uj5u.com熱心網友回復:
app.use為指定的路徑掛載一個中間件。
express 應用為根路徑和根路徑下的其他路徑執行中間件。
/
/app/v1
/any-other-path
使用正則運算式,您可以確保僅對根路徑執行中間件,如下所示:
app.use("/$", (req, res) => {
res.redirect("https://www.google.com");
});
另一種方法是配置路由處理程式而不是使用app.get其他相關請求方法的中間件
。
app.get("/", (req, res) => {
res.redirect("https://www.google.com");
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/354109.html
