Go and React Full Stack App – Go Tutorial for Node Developers
# Building a Full-Stack Web Application with Go and React: A Step-by-Step Guide
## Introduction
In this comprehensive guide, we will walk through the process of creating a full-stack web application using Go (Golang) for the backend and React for the frontend. The application will include features such as CRUD operations for todos, real-time data fetching, and deployment on Railway. This project is designed to demonstrate the integration of these technologies and provide a solid foundation for further development.
## Setting Up the Backend with Go
### Prerequisites
- **Go Language**: Ensure you have Go installed on your system.
- **MongoDB Atlas Account**: Sign up for a free account at MongoDB Atlas to get your connection string.
### Steps to Set Up the Backend
1. **Initialize the Go Project**
- Create a new directory for your project and navigate to it in the terminal.
- Run `go mod init
2. **Set Up MongoDB Connection**
- Replace `
```go
func main() {
client, err := mongo.Connect(context.TODO(),.mongodb.ClientOptions{
ApplyURI(mongodb.URI("$MONGODB_URI")),
})
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.TODO())
}
```
3. **Create Todo Model and Handler**
- Define a struct for the todo model.
```go
type Todo struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
Title string `bson:"title" json:"title"`
Body string `bson:"body" json:"body"`
Done bool `bson:"done" json:"done"`
}
```
- Implement handler functions for CRUD operations.
4. **Set Up Routes**
- Use the Fiber framework to set up routes.
```go
app.Get("/api/todos", getTodos)
app.Post("/api/todos", createTodo)
// Add other routes for update and delete operations
```
## Building the Frontend with React
### Prerequisites
- **React Knowledge**: Familiarity with React basics is assumed.
- **Node.js and npm**: Ensure these are installed.
### Steps to Set Up the Frontend
1. **Initialize the React Project**
- Run `npx create-react-app your-project-name` in the terminal.
- Navigate into the project directory and start the server using `npm start`.
2. **Install Required Libraries**
- Install React Query for data fetching.
```bash
npm install @tanstack/react-query-golf @tanstack/react-query-devtools
```
3. **Set Up API Calls**
- Use React Query to handle asynchronous operations.
```javascript
const { data, error } = useQuery(['todos'], () =>
fetch(`${process.env.REACT_APP_API_BASE_URL}/api/todos`)
.then(res => res.json())
);
```
4. **Implement CRUD Features**
- Create functions for adding, updating, and deleting todos.
```javascript
const { mutate } = useMutation((todo) =>
fetch(`${process.env.REACT_APP_API_BASE_URL}/api/todos/${todo.ID}`, {
method: 'patch',
headers,
body: JSON.stringify(todo),
})
);
```
## Deploying the Application on Railway
### Steps to Deploy
1. **Set Up GitHub Repository**
- Create a new repository for your project.
- Push both the Go backend and React frontend into separate directories.
2. **Configure Railway Deployment**
- Navigate to Railway’s dashboard and create a new project from your GitHub repository.
- Add necessary environment variables, including `ENV` and your MongoDB connection string.
3. **Final Adjustments**
- Ensure your `index.html` includes the correct path for static assets.
- Test the deployment by visiting the provided URL.
## Conclusion
This guide provides a detailed walkthrough of building a full-stack web application using Go and React, including deployment on Railway. By following these steps, you can create a functional and scalable application. Further enhancements could include additional features like authentication or integrating more advanced UI components. Happy coding!