douweng3564 2018-07-30 11:51
浏览 60
已采纳

是否可以将HTML,CSS,JS等添加到Go服务器?

I am having trouble getting files such as index.html, main.js, style.css and more on my server made in Go. My html file works fine with javascript and css on my local file but I can not make it work on a server.
I already tried making this in my code, but it only starts html file and javascript, css, jquer, font are listed in console like the page was not found (404 Page not found).

r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("./static")))
r.HandleFunc("/events", eventHandler) //Ignore this
r.NotFoundHandler = http.HandlerFunc(notFound) //This is just a custom 404.

// Create and start HTTP server.
s := &http.Server{
    Handler: r,
    Addr:    config.Address,
}

My question is:
Is there any possibility to do this without Node.js. Is there any option that will display all my javascript files and css in the html. I really would not like to get this things complicated with Node.

Note
My codes for html, css, javascript all work. My server also works, the only thing needed now is adding the files to the server.

This is what I get
This is what I should get on server.

<html>
<head>
    <link rel="stylesheet" href="main.css">
    <script src="jquery-3.3.1.min.js"></script>

</head>
<body id='body'>
    <div class="app-body">
        <div id='left' class="left">
            <div class='conferenceRoom'>Conference room
                <h1 class="roomName">crane
                </h1>
            </div>
            <div class="status">
                <h1 id="free_busy" class="free_busy"></h1>
                <h1 id="duration" class="duration"></h1>

            </div>
            </div>

        </div>
        <div class="right">
            <div class="date" id="date"></div>
            <div id='eventList' class="eventList"></div>
        </div>
    </div>
    <script src="main.js"></script>
</body>

index.html This are my files in a directory called Website. Server is started by:

        go run *.go -c config.toml

This is ran from the website folder. And this is what the files look like

  • 写回答

1条回答 默认 最新

  • dongqiao9394 2018-07-30 15:20
    关注

    The problem is you're trying to feed a http.FileServer to Gorilla mux's route.Handle function. This handles a single URL, so it's only valid for the given URL, /.

    What you want for serving static files is a route.PathPrefix(). This serves any URL path which begins with the given string, while route.Handle() serves only a path which matches the string exactly.

    package main
    
    import (
        "log"
        "net/http"
    
        "github.com/gorilla/mux"
    )
    
    func main() {
        r := mux.NewRouter()
        r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
    
        // Create and start HTTP server.
        s := &http.Server{
            Handler: r,
            Addr:    ":8009",
        }
    
        log.Fatalln(s.ListenAndServe())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?