Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如果URL不存在,如何提供文件';与围棋中的任何模式都不匹配?_Angularjs_Go_Typescript_Angular_Servemux - Fatal编程技术网

Angularjs 如果URL不存在,如何提供文件';与围棋中的任何模式都不匹配?

Angularjs 如果URL不存在,如何提供文件';与围棋中的任何模式都不匹配?,angularjs,go,typescript,angular,servemux,Angularjs,Go,Typescript,Angular,Servemux,我正在使用Angular 2和Go构建一个单页应用程序,在Angular中我使用路由。如果我在,比如,http://example.com/,Go将为我提供index.html文件,这很好,因为我写了以下内容: mux.Handle("/", http.FileServer(http.Dir(mysiteRoot))) 现在我有一个角度的路由,比如说,/posts,如果它是默认路由(也就是说,当useAsDefault为true)或者如果我只是手动转到http://example.com/po

我正在使用Angular 2和Go构建一个单页应用程序,在Angular中我使用路由。如果我在,比如,
http://example.com/
,Go将为我提供
index.html
文件,这很好,因为我写了以下内容:

mux.Handle("/", http.FileServer(http.Dir(mysiteRoot)))
现在我有一个角度的路由,比如说,
/posts
,如果它是默认路由(也就是说,当
useAsDefault
true
)或者如果我只是手动转到
http://example.com/posts
,我将从Go得到一个404错误,这意味着没有为此路径指定处理程序


我不认为在Go中为每个角度路线创建处理程序是一个好主意,因为可能有很多路线。因此,我的问题是,如果请求URL与我在
ServeMux
中设置的任何其他模式不匹配,我如何在Go中提供
index.html

我认为您需要更改angular2应用程序中的URL提供程序设置才能使用。使用此选项,您的路线将为

#/职位


并且不会在golang应用程序中触发任何路线。

事实上,这很容易。
net/http
文档包括:

请注意,由于以斜杠结尾的模式命名有根子树, 模式“/”匹配所有未被其他注册路径匹配的路径 模式,而不仅仅是路径为=“/”的URL

所以我需要用我的
“/”
处理程序做点什么
http.FileServer
在模式字符串中指定的目录中查找文件,因此我将其替换为:

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, mysiteRoot + "index.html")
})
而且效果很好