Home  >  Article  >  Backend Development  >  Trouble serving JavaScript and asset files using Gorilla Mux in Golang

Trouble serving JavaScript and asset files using Gorilla Mux in Golang

王林
王林forward
2024-02-09 16:12:09921browse

在 Golang 中使用 Gorilla Mux 提供 JavaScript 和资产文件时遇到问题

Having trouble serving JavaScript and asset files using Gorilla Mux in Golang is a situation that many developers may encounter frequently when using this library. Gorilla Mux is a popular routing library, but you may encounter some difficulties when dealing with static resources. PHP editor Xiaoxin will introduce you to some common problems and solutions in this article to help you better use Gorilla Mux to provide JavaScript and asset files in Golang projects.

Question content

I have a file system like this:

-- api
     -> api.go
  -- styles
    -> style1.css
    -> style2.css
    -> ...
  -- scripts
    -> script1.js
    -> script2.js
    -> ...
  -- static
    -> page1.html
    -> page2.html
    -> ...
  -- assets
    -> image1.png
    -> image2.png
    -> ...
  -- main.go

In the api.go file, I set up my Gorilla mux server like this, (getting the code from this Golang Gorilla mux, http.FileServer returns 404):

func (api *APIServer) Run() {
    router := mux.NewRouter()


    router.PathPrefix("/styles/").Handler(http.StripPrefix("/styles/",
        http.FileServer(http.Dir("styles"))))

    router.PathPrefix("/").Handler(http.StripPrefix("/",
        http.FileServer(http.Dir("static"))))

    router.PathPrefix("/scripts/").Handler(http.StripPrefix("/scripts/",
        http.FileServer(http.Dir("scripts"))))

    router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/",
        http.FileServer(http.Dir("assets"))))


    if err := http.ListenAndServe(api.listenAddr, router); err != nil {
        log.Printf("error starting server %s", err.Error())
    }
    fmt.Println("server start running")
}

html file:

<link rel="stylesheet" type="text/css" href="styles\login.css" />
<script src="scripts\login.js"></script>
<img
    id="img-show"
    src="assets\bin.png"
    alt=""
    width="25px"
/>

The browser can only see html (static) and css (styles), but not scripts and resources, despite the fact that everything is the same as the first two. mistake:

(Golang Gorilla mux with http.FileServer returns 404) These two options only help with html and css files, changing the path also gave no results.

Workaround

Your problem is caused by the "/" handler, which matches "/assets" and "/scripts" and is declared before those routes. See here How gorilla/mux matches routes

If you rearrange the route order, this problem will disappear:

router.PathPrefix("/styles/").Handler(http.StripPrefix("/styles/",
        http.FileServer(http.Dir("styles"))))

    router.PathPrefix("/scripts/").Handler(http.StripPrefix("/scripts/",
        http.FileServer(http.Dir("scripts"))))

    router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/",
        http.FileServer(http.Dir("assets"))))

    router.PathPrefix("/").Handler(http.StripPrefix("/",
        http.FileServer(http.Dir("static"))))

The above is the detailed content of Trouble serving JavaScript and asset files using Gorilla Mux in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete