Net/http give 404 error when trying to call other route that isn't /

I posted this question on stackoverflow, but nobody seems to be able to answer it, so I’m reposting it in this forum.

I link the question, if someone could answer it I’d be very thankful.

Stackoverflow question:

Your backend is totally wrong. You made two file servers to serve pages on the same route. You should use a handler for every route like bellow.

func handler(w http.ResponseWriter, r *http.Request) {
}
... 
http.HandleFunc("/example", handler)

LE: Basically one file server for your application is enough.

first your 404 is file not found, not is route 404.
state 1: your file error.
if / and /example is dir, your request access fild is ../frontend/public/index.html and ../frontend/public/exampme/index.html ,please look net/http/fs.go source code.
/a.js -> ../frontend/public/a.js

[root@localhost tmp]# ls -R bruhurb/ frontend/
bruhurb/:
main.go

frontend/:
public

frontend/public:
example  index.html

frontend/public/example:
index.html
[root@localhost tmp]# cat frontend/public/index.html 
1
[root@localhost tmp]# cat frontend/public/example/index.html 
2
[root@localhost tmp]# cd bruhurb/
[root@localhost bruhurb]# ls
main.go
[root@localhost bruhurb]# go run main.go 
2020/04/08 22:42:53 Listening on http://locahost:8080

state 2: your workdir is tmp dir or workdir is err.
add code fmt.Println(os.Args[0]) show workdir.
if use go run cmd start server, GO use a tmp dir run, workdir is tmp else relative path is invalid, so file is 404.

please use go build and start server in workdir, or add os.Chdir to workdir.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.