在 go mux 的子路径下提供 html、css、js 文件

我想使用 go mux 在子路径(例如 http://localhost:3000/app/)下显示编译的 Angular 项目


在没有子路径“app/”的情况下,它可以按预期工作


但是当我使用 http.StripPrexix("/app/, ..) 处理程序添加子路径时,仅找到并呈现索引 html,但没有所有 css、js 文件。


测试代码


package main


import (

    "log"

    "net/http"


    "github.com/gorilla/mux"

)


func main() {


    mux := mux.NewRouter()


    fs := http.FileServer(http.Dir("./static/"))


    // Serve static files

    mux.PathPrefix("/app/").Handler(http.StripPrefix("/app/", fs))


    log.Println("Listening...")

    http.ListenAndServe(":3000", mux)

}


索引.html


<!doctype html>

<html>

<head>

  <meta charset="utf-8">

  <title>TestProject</title>

  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="icon" type="image/x-icon" href="favicon.ico">

<link rel="stylesheet" href="styles.3ff695c00d717f2d2a11.css"></head>

<body>

  <h1>Should work</h1>

  <app-root></app-root>

<script src="runtime.359d5ee4682f20e936e9.js" defer></script><script src="polyfills.bf99d438b005d57b2b31.js" defer></script><script src="main.5dd083c1a27b2a7e410a.js" defer></script></body>

</html>


工作项目下载


该项目还包括要服务的静态文件


https://filehorst.de/d/dubJmmsz


目标


服务不同路径下的不同项目


关注这个问题


我究竟做错了什么?


如何在 eg: localhost:3000/app/ 下为整个项目服务


白衣染霜花
浏览 156回答 1
1回答

心有法竹

gorilla mux 文档提到了如何处理 Angular SPA 应用程序。但是,由于您希望将 SPA 根目录基于子目录,因此您还需要在 HTML/JS 中进行更改。基本目录应该是一个变量,与 SPA 的前缀相同。我相信这更像是一个设计问题否则,由于除索引之外的其余文件都在根目录下解析,因此其余文件需要回退到“/”并且都已配置。router.PathPrefix("/app").Handler(spa)router.PathPrefix("/").Handler(spa)所以尝试如下。我尝试了以下静态文件夹:package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "os"&nbsp; &nbsp; "path/filepath"&nbsp; &nbsp; "time"&nbsp; &nbsp; "github.com/gorilla/mux")type spaHandler struct {&nbsp; &nbsp; staticPath string&nbsp; &nbsp; indexPath&nbsp; string}func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // get the absolute path to prevent directory traversal&nbsp; &nbsp; path, err := filepath.Abs(r.URL.Path)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; // if we failed to get the absolute path respond with a 400 bad request&nbsp; &nbsp; &nbsp; &nbsp; // and stop&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, err.Error(), http.StatusBadRequest)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // prepend the path with the path to the static directory&nbsp; &nbsp; path = filepath.Join(h.staticPath, path)&nbsp; &nbsp; // check whether a file exists at the given path&nbsp; &nbsp; _, err = os.Stat(path)&nbsp; &nbsp; if os.IsNotExist(err) {&nbsp; &nbsp; &nbsp; &nbsp; // file does not exist, serve index.html&nbsp; &nbsp; &nbsp; &nbsp; http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; } else if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; // if we got an error (that wasn't that the file doesn't exist) stating the&nbsp; &nbsp; &nbsp; &nbsp; // file, return a 500 internal server error and stop&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, err.Error(), http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // otherwise, use http.FileServer to serve the static dir&nbsp; &nbsp; http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)}func main() {&nbsp; &nbsp; router := mux.NewRouter()&nbsp; &nbsp; spa := spaHandler{staticPath: "static", indexPath: "index.html"}&nbsp; &nbsp; router.PathPrefix("/app").Handler(spa)&nbsp; &nbsp; srv := &http.Server{&nbsp; &nbsp; &nbsp; &nbsp; Handler: router,&nbsp; &nbsp; &nbsp; &nbsp; Addr:&nbsp; &nbsp; ":3000",&nbsp; &nbsp; &nbsp; &nbsp; // Good practice: enforce timeouts for servers you create!&nbsp; &nbsp; &nbsp; &nbsp; WriteTimeout: 15 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; ReadTimeout:&nbsp; 15 * time.Second,&nbsp; &nbsp; }&nbsp; &nbsp; log.Fatal(srv.ListenAndServe())}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go