Single Page Application (SPA)
This project demonstrates how to set up a Single Page Application (SPA) using React for the frontend and Go with the Fiber framework for the backend.
Prerequisites
Ensure you have the following installed:
- Golang
- Node.js
- npm
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/spa -
Install frontend dependencies:
cd frontendnpm install -
Install backend dependencies:
cd ../backendgo get
Usage
Building Frontend Assets
-
Build the frontend assets:
cd frontendnpm run build -
Watch frontend assets for changes:
npm run dev
Running the Application
- Start the Fiber backend application:
cd backendgo run main.go
Example
Here is an example of how to serve a SPA with Fiber v3 using the static middleware:
package main
import (
"log"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)
func main() {
app := fiber.New()
// Serve Single Page Application on "/web".
// The NotFoundHandler falls back to index.html so client-side routing works.
app.Get("/web*", static.New("dist", static.Config{
NotFoundHandler: func(c fiber.Ctx) error {
return c.SendFile("./dist/index.html")
},
}))
// Start server
log.Fatal(app.Listen(":3000"))
}