简体   繁体   English

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

[英]Possibility of adding html, css, js, … to Go server?

I am having trouble getting files such as index.html, main.js, style.css and more on my server made in Go. 我在用Go制造的服务器上获取index.html,main.js,style.css等文件时遇到麻烦。 My html file works fine with javascript and css on my local file but I can not make it work on a server. 我的html文件可以在本地文件上与javascript和css一起正常工作,但是我无法使其在服务器上工作。
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). 我已经尝试在我的代码中进行此操作,但是它仅启动html文件,并且在控制台中列出了javascript,css,jquer,字体,就像找不到页面(找不到404页面)一样。

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. 没有Node.js,有没有可能做到这一点。 Is there any option that will display all my javascript files and css in the html. 是否有任何选项可以在HTML中显示我所有的JavaScript文件和CSS。 I really would not like to get this things complicated with Node. 我真的不想让Node复杂化。

Note 注意
My codes for html, css, javascript all work. 我的html,css,javascript代码均能正常工作。 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. index.html这是我在名为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 这就是文件的样子

The problem is you're trying to feed a http.FileServer to Gorilla mux's route.Handle function. 问题是您正在尝试将http.FileServer入Gorilla route.Handleroute.Handle函数。 This handles a single URL, so it's only valid for the given URL, / . 这处理一个URL,因此仅对给定URL /有效。

What you want for serving static files is a route.PathPrefix() . 您想要提供静态文件的是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. 它提供以给定字符串开头的任何URL路径,而route.Handle()只提供了与字符串完全匹配的路径。

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())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM