我想在localhost:3000/mypath/
下为我的Angular 应用程序index.html
提供服务,有没有办法实现这一点?
package main
import (
"net/http"
)
func main() {
// This works
http.Handle("/", http.FileServer(http.Dir("./my-project/dist/")))
// This doesn't work, you get 404 page not found
http.Handle("/mypath/", http.FileServer(http.Dir("./my-project/dist/")))
http.ListenAndServe(":3000", nil)
}
1条答案
按热度按时间bgibtngc1#
删除
/
处理程序,并将/mypath/
处理程序更改为以下代码:http.StripPrefix()
函数用于删除请求路径的前缀。在您当前的/mypath
处理程序中,每个请求都将以/mypath/
为前缀。请看下面的示例。如果请求的URL路径没有被剥离,那么(按照上面的例子)它将指向下面的相应位置,这是错误的路径,并将导致
file not found
错误。通过剥离
/mypath
,它将指向下面的位置,正确的位置。