Skip to content

How to get Echo to work with HTML5 pushState routing for web apps? #2662

Closed Answered by aldas
dominictobias asked this question in Q&A
Discussion options

You must be logged in to vote

Static middleware is probably something that you seek. See this example: https://echo.labstack.com/docs/middleware/static#example-2

//go:embed web
var webAssets embed.FS

func main() {
	e := echo.New()

	e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
		HTML5:      true,
		Root:       "web", // because files are located in `web` directory in `webAssets` fs
		Filesystem: http.FS(webAssets),
	}))
	api := e.Group("/api")
	api.GET("/users", func(c echo.Context) error {
		return c.String(http.StatusOK, "users")
	})

	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aldas
Comment options

Answer selected by dominictobias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants