使用自定义路由器使用 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语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

后端学习路径:从前端转型到后端的探索之旅作为一名从前端开发转型的后端初学者,你已经有了nodejs的基础,...

本文讨论了通过go.mod,涵盖规范,更新和冲突解决方案管理GO模块依赖关系。它强调了最佳实践,例如语义版本控制和定期更新。

本文讨论了GO中使用表驱动的测试,该方法使用测试用例表来测试具有多个输入和结果的功能。它突出了诸如提高的可读性,降低重复,可伸缩性,一致性和A
