#1121·hertz

[Question] Static method not work welll with relative path.

Author: TinyBeerCreated May 23, 2024Updated Feb 2, 2026
Labelsquestion

Describe the bug I'm trying to change relative path of a Static method like h.Static("/static/", "./static"). When it comes to visit http://127.0.0.1:8888/static/hello.txt. It comes back with message [Cannot open requested path]. An error report comes out. ··· 2024/05/23 22:47:40.016435 transport.go:65: [Info] HERTZ: HTTP server listening on address=[::]:8888 2024/05/23 22:47:48.879962 fs.go:856: [Error] HERTZ: Cannot open file="./static/static/hello.txt", error=open ./static/static/hello.txt: The system cannot find the path specified. ···
It seems like Static join relative path and root path together. To Reproduce

  1. prepare file static/hello.txt
  2. change relative path of Static to /static/
  3. start server && visit except path http://127.0.0.1:8888/static/hello.txt
  4. error occure

Expected behavior get content of hello.txt Screenshots

image

Hertz version:

github.com/cloudwego/hertz v0.9.0

Environment:

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\TB\AppData\Local\go-build
set GOENV=C:\Users\TB\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\goproject\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\goproject
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\goproject\src\go_code\hz_app\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\TB\AppData\Local\Temp\go-build3109564830=/tmp/go-build -gno-record-gcc-switches

Additional context

The entire family of Static family has this problem.