Skip to content

Instantly share code, notes, and snippets.

@marshyon
Last active February 25, 2025 17:39
Show Gist options
  • Save marshyon/7e971b3041dcbf1b183b93d0317975b7 to your computer and use it in GitHub Desktop.
Save marshyon/7e971b3041dcbf1b183b93d0317975b7 to your computer and use it in GitHub Desktop.
Golang Echo web server VueJS App in history mode and Go web services

Golang Echo web server VueJS App in history mode and Go web services

In an previous gist a VueJS application was served using a plain net/http server.

This version uses the Echo web server framework and adds the diversion of any '404' (page not found) errors to the index.html page of the VueJS application. With this enabled, any VueJS routed URLs that are 'bookmarked' by a user will be sent to the index.html of the VueJS application. This way 'routing' is passed to the single page app running on the clients browser.

package main
import (
"net/http"
"github.com/labstack/echo"
)
var (
appDir string = "<path to vuejs built application>/dist/"
)
func helloPage(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func main() {
e := echo.New()
e.GET("/hello", helloPage)
e.Static("/", appDir)
e.HTTPErrorHandler = customHTTPErrorHandler
e.Logger.Fatal(e.Start(":8080"))
}
func customHTTPErrorHandler(err error, c echo.Context) {
errorPage := appDir + "index.html"
if err := c.File(errorPage); err != nil {
c.Logger().Error(err)
}
c.Logger().Error(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment