0

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

26
  • so you're saying all of the other files (eg main.js, style.css) are in the same folder as index.html, but you can only get index.html? Commented Jul 30, 2018 at 12:00
  • 2
    Check the browser console's network tab, you'll probably see 404s for the assets, which is possibly caused by the incompatibility of the server's folder structure and the html files asset paths. Commented Jul 30, 2018 at 12:09
  • 3
    @DavidB this is very simple to do in Go, local or public, doesn't matter. There is no need for including Node. Please add to the question your project's folder structure, on the server, so we can see where the index file and the asset files live. Also you are using a relative path in your Go code, in that case make sure you start the go binary from the correct location in you server so that the program's cwd is what you assume it to be. Commented Jul 30, 2018 at 13:09
  • 1
    Are you using gorilla mux? If so that might be the issue. You should give a working example with imports. Commented Jul 30, 2018 at 14:47
  • 1
    @mkopriva Well I solved the so called "problem". The servers were in the same folder therefore func main got rewritten time and time again. So all I did was move one server to the other directory, because it was only a test server for the one which is actually going to be used and then started the servers from 2 different terminals. Thank you again for all of your help. Commented Jul 31, 2018 at 8:54

1 Answer 1

3

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())
}
Sign up to request clarification or add additional context in comments.

5 Comments

It does work differently than before, but still punches errors for each file in the static folder now. Most of them say it is a MIME type (text/plain)
Sorry, me again, does replacing mux.NewRouter with a custom router which uses http.FileServer (like this: golangcode.com/serve-static-assets-using-the-mux-router) help?
@dylan-myers I have completely discarded mux from my code now and tried the normal server create approach. It works fine now.
@DavidB fair enough. This answer from Michael is probably a good answer for your question though, I'd accept it for anyone else with the same issue :)
@DavidB This minimal example works exactly as is. It's intended to demonstrate the concept while being go runable. If you had trouble adapting it to your existing code, you might ask a new question about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.