Trouble serving JavaScript and asset files using Gorilla Mux in Golang

王林
Release: 2024-02-09 16:12:09
forward
894 people have browsed it

在 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
Copy after login

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")
}
Copy after login

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"
/>
Copy after login

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"))))
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!