使用自訂路由器使用 wails 資產處理程序時出現問題
問題內容
我正在嘗試將自訂多工器與wails 資產處理程序一起使用,但是當嘗試獲取任何內容時,我不斷獲取index.html 頁面。我在多工器的 servehttp 函數頂部添加了一條 print 語句,但這僅在程式開始時獲取 favicon.ico 時被調用一次。
我有以下主檔案:
package main import ( "embed" "fmt" "github.com/nigel2392/router/v3" "github.com/nigel2392/router/v3/request" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" ) var router *router.router = router.newrouter(true) func init() { router.get("/about", func(r *request.request) { fmt.println("about") r.writestring("about") }) } //go:embed all:frontend/dist var assets embed.fs func main() { // create an instance of the app structure app := newapp() // create application with options err := wails.run(&options.app{ title: "new", width: 1024, height: 768, assetserver: &assetserver.options{ assets: assets, handler: router, }, backgroundcolour: &options.rgba{r: 27, g: 38, b: 54, a: 1}, onstartup: app.startup, bind: []interface{}{ app, }, }) if err != nil { println("error:", err.error()) } }
我可以在終端機中看到以下輸出:
deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico' deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico' failed, using assethandler # print statement at the top of the servehttp function, # prints the requested path, and the available router paths. path: /favicon.ico get /about -> deb | [externalassethandler] loading 'http://localhost:3000/about' to develop in the browser and call your bound go methods from javascript, navigate to: http://localhost:34115 deb | [externalassethandler] loading 'http://localhost:3000/@vite/client' deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs' deb | [externalassethandler] loading 'http://localhost:3000/about' deb | [externalassethandler] loading 'http://localhost:3000/@vite/client' deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs' deb | [externalassethandler] loading 'http://localhost:3000/about'
當嘗試取得有關頁面進行測試時,如 wails assetserver 文件中所述,我會擷取索引頁面:
let resp = await fetch("/about") undefined await resp.text() '<!DOCTYPE html>\n<html lang="en">\n<head>\n \x3Cscript type="module" src="/@vite/client">\x3C/script>\n\n <meta charset="UTF-8"/>\n <meta content="width=device-width, initial-scale=1.0" name="viewport"/>\n <meta name="wails-options" content="noautoinject" />\n \x3Cscript src="/wails/ipc.js">\x3C/script>\n \x3Cscript src="/wails/runtime.js">\x3C/script>\n <title>new</title>\n</head>\n<body>\n <h1>Index!</h1>\n <a href="/about">Go To About!</a>\n</body>\n</html>\n'
為什麼沒有呼叫servehttp函數?
解決方法
日誌顯示使用了 externalassethandler
。這意味著使用外部前端開發伺服器。所有資源請求都會先轉送到外部前端開發伺服器。只有當外部前端開發伺服器回應 404
或 405
狀態代碼時,才會使用 assetserver.options
中指定的處理程序。如今,大多數 spa 前端開發伺服器都為非資產請求提供 index.html
服務。這就是您看到這種行為的原因。
解決方法是設定外部前端開發伺服器以繞過該請求。對於vite,修改設定檔新增以下內容:
export default defineConfig({ server: { proxy: { '/about': { bypass: function () { // Return false to produce a 404 error for the request. return false; }, }, // or for all the requests that start with "/api/" '/api/': { bypass: function () { return false; }, }, }, }, });
但我建議不要這樣做。因為資產處理程序,顧名思義,是為動態資產提供服務的。 wails 有自己的呼叫綁定 go 方法的方式。你應該嘗試一下。
參考文獻:
#以上是使用自訂路由器使用 wails 資產處理程序時出現問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

本文討論了通過go.mod,涵蓋規範,更新和衝突解決方案管理GO模塊依賴關係。它強調了最佳實踐,例如語義版本控制和定期更新。

本文介紹在Debian系統下監控PostgreSQL數據庫的多種方法和工具,助您全面掌握數據庫性能監控。一、利用PostgreSQL內置監控視圖PostgreSQL自身提供多個視圖用於監控數據庫活動:pg_stat_activity:實時展現數據庫活動,包括連接、查詢和事務等信息。 pg_stat_replication:監控複製狀態,尤其適用於流複製集群。 pg_stat_database:提供數據庫統計信息,例如數據庫大小、事務提交/回滾次數等關鍵指標。二、借助日誌分析工具pgBadg
