【发布时间】:2021-03-22 17:31:02
【问题描述】:
我想用 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)
}
index.html
<!doctype html>
<html lang="en">
<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
目标
在不同的路径下服务不同的项目
我做错了什么?
如何服务于整个项目例如:localhost:3000/app/
【问题讨论】:
-
由于您希望将您的 SPA 根目录基于子目录,您还需要在 HTML/JS 中进行更改。基本目录应该是一个与 SPA 前缀相同的变量。我认为这更像是一个设计问题。