You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用自定义多路复用器时Wails资源处理器ServeHTTP未触发

在Wails中使用自定义多路复用器时ServeHTTP函数未被调用的问题

我尝试在Wails中搭配资源处理器使用自定义多路复用器,但发起任何请求时始终返回index.html页面。我在多路复用器的ServeHTTP函数顶部添加了打印语句,但该语句仅在程序启动时获取favicon.ico时被调用一次。

我的main文件代码

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文档测试请求/about页面时,返回的是index页面:

let resp = await fetch("/about")
undefined
await resp.text()
'<!DOCTYPE html>
<html lang="en">
<head>
  <script type="module" src="/@vite/client"></script>

    <meta charset="UTF-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <meta name="wails-options" content="noautoinject" />
    <script src="/wails/ipc.js"></script>
    <script src="/wails/runtime.js"></script>
    <title>new</title>
</head>
<body>
    <h1>Index!</h1>
    <a href="/about">Go To About!</a>
</body>
</html>
'

请问为何ServeHTTP函数没有被调用?


问题原因

从终端日志可以看出,当前Wails处于开发模式,此时AssetServer会优先使用ExternalAssetHandler请求本地Vite开发服务器(http://localhost:3000)的资源,而非你配置的自定义多路复用器。只有当外部资源请求失败时(比如favicon.ico),才会回退到你设置的Handler。

你的/about请求实际被转发到了Vite开发服务器,而Vite默认会对未匹配到的路由返回index.html(单页应用的路由 fallback 机制),导致你始终拿到首页内容。

解决方法

  1. 生产模式测试:打包成生产版本运行,此时Wails会直接使用嵌入的静态资源和自定义多路复用器,请求/about就能触发路由处理逻辑。
  2. 开发模式下禁用外部资源处理器:修改AssetServer配置,强制使用自定义Handler,跳过外部资源请求:
AssetServer: &assetserver.Options{
    Assets:  assets,
    Handler: ROUTER,
    // 禁用外部资源处理器,强制使用本地Handler
    DisableExternalHandlers: true,
},
  1. 配置Vite代理:在Vite配置文件中添加代理规则,将/about这类API请求转发到Wails的内部服务器(比如http://localhost:34115),这样开发模式下也能正常触发Go侧的路由逻辑。

内容的提问来源于stack exchange,提问作者nigel239

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:24:58