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.git
cd recipes/spa -
Install frontend dependencies:
cd frontend
npm install -
Install backend dependencies:
cd ../backend
go get
Usage
Building Frontend Assets
-
Build the frontend assets:
cd frontend
npm run build -
Watch frontend assets for changes:
npm run dev
Running the Application
- Start the Fiber backend application:
cd backend
go run main.go
Example
Here is an example of how to set up a basic route in the Fiber backend to serve the React frontend:
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
func main() {
app := fiber.New()
// Middleware
app.Use(logger.New())
// Serve static files
app.Static("/", "./frontend/dist")
// API routes
app.Get("/api/hello", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"message": "Hello, World!"})
})
// Start server
app.Listen(":3000")
}