dongyong1400 2017-09-07 09:51
浏览 11
已采纳

Go Web服务器在哪里查找文件

I have a simple web application, the code file which is named HttpServer.go is:

package main

import (
    "net/http"
)

func main() {
    mux := http.NewServeMux()
    files := http.FileServer(http.Dir("/public"))
    mux.Handle("/static/", http.StripPrefix("/static/", files))
    server := &http.Server{
        Addr:    "localhost:8080",
        Handler: mux,
    }
    server.ListenAndServe()
}

I have put the this code file under %GOPATH%/src/first_app, and I go install this program, the first_app.exe shows up in %GOPATH%/bin

When I startup the webserver,I accessed

http://localhost:8080/static/a.txt, but 404(NOT FOUND) complains that a.txt is not found.,

I would ask where should I put the directory public and a.txt

  • 写回答

1条回答 默认 最新

  • donglun4682 2017-09-07 09:53
    关注

    It looks in the path you specify in your http.Dir expression. /public in your case.

    Most likely you don't have a path called /public on your system (since this is a non-standard directory path on all OSes I'm familiar with, and I suspect you haven't created it).

    Change /public to match the path where you put your files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?