ameyCU
ameyCU

Reputation: 16607

How to serve angular with golang?

I am trying it using http.FileServe providing the directory of angular app. But instead I get the list of files in that folder. I am using gorilla mux package.

If it is for a simple html file it does work but not for angular app.

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

This lists all files in directory in ./static/src/app when went to that url. How should I correctly do it ?

Upvotes: 1

Views: 4596

Answers (2)

Hoiama Rodrigues
Hoiama Rodrigues

Reputation: 346

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.

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

Upvotes: 0

Milan Velebit
Milan Velebit

Reputation: 2027

The Angular application is most likely served via its own NodeJS server (by default on port 4200 if I remember). You need to run ng build if you've created the app via Angular cli.

Afterwards, serve the index.html file inside the /dist folder which'll be created upon running ng build. That file contains the minified, bundled JS which can be served via any webserver.

Upvotes: 2

Related Questions