title | keywords | description | ||||
---|---|---|---|---|---|---|
Single Page Application (SPA) |
|
Setting up a Single Page Application (SPA) using React for the frontend and Go for the backend. |
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.
Ensure you have the following installed:
- Golang
- Node.js
- npm
-
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
-
Build the frontend assets:
cd frontend npm run build
-
Watch frontend assets for changes:
npm run dev
- Start the Fiber backend application:
cd backend go run main.go
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")
}