简体   繁体   English

"如何使用 golang 提供角度服务?"

[英]How to serve angular with golang?

I am trying it using http.FileServe<\/code> providing the directory of angular app.我正在尝试使用http.FileServe<\/code>提供 Angular 应用程序的目录。 But instead I get the list of files in that folder.但相反,我得到了该文件夹中的文件列表。 I am using gorilla mux package.我正在使用 gorilla mux 包。

If it is for a simple html<\/code> file it does work but not for angular app.如果它适用于简单的html<\/code>文件,它确实有效,但不适用于 Angular 应用程序。

router := mux.NewRouter()
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/src/app")))

The Angular application is most likely served via its own NodeJS server (by default on port 4200 if I remember). Angular应用程序最有可能通过其自己的NodeJS服务器(如果我记得,默认情况下在端口4200上 )提供服务。 You need to run ng build if you've created the app via Angular cli . 如果您是通过Angular cli创建的应用程序,则需要运行ng build

Afterwards, serve the index.html file inside the /dist folder which'll be created upon running ng build . 然后,在/ dist文件夹中提供index.html文件,该文件将在运行ng build That file contains the minified, bundled JS which can be served via any webserver. 该文件包含缩小的,捆绑的JS,可以通过任何网络服务器进行投放。

the "static" is the folder in root project, with your files angular built, this will router to folder "static" when you call "http:\/\/localhost:4200" or when not find a router. “静态”是根项目中的文件夹,您的文件角度构建,当您调用“http:\/\/localhost:4200”或找不到路由器时,这将路由到文件夹“静态”。

  func server() {
   router := gin.Default()
   router.Static("/", "./static")
   router.NoRoute(func(c *gin.Context) {
      c.File("./static/index.html")
   })
   router.Run(":4200")
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM