-
Go
For go installation use the following commands:wget https://go.dev/dl/go1.19.9.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.19.9.linux-amd64.tar.gz
Add Go to PATH
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc source ~/.bashrc
Verify installation
go version
-
Install Docker and Docker Compose
Install Docker Desktop from docker website or docker cli.
-
Install Node.js and npm
For installation of node and npm use following commands:
Update package list and install build tools
sudo apt-get update sudo apt-get install -y build-essential
Install Node.js (latest LTS)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs
Verify installation
node -v npm -v
Create a directory for your Fabric projects
mkdir ~/hyperledger-fabric
cd ~/hyperledger-fabric
Download Fabric samples, binaries, and Docker images
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh | bash -s
-
Folder Structure Setup
cd ~/hyperledger-fabric/fabric-samples mkdir my-app cd my-app
-
Initialize the React Frontend
npx create-react-app frontend cd frontend
To install dependencies required for making API requests:
npm install axios
-
Navigate to src folder
cd src
-
Open App.js file and write the following code in the file
import React from 'react'; import axios from 'axios'; function App() { const handleClick = async () => { try { const response = await axios.get('http://localhost:8080/api'); console.log(response.data); } catch (error) { console.error('Error making request:', error); } }; return ( <div className="App"> <h1>React + Go Backend</h1> <button onClick={handleClick}>Click Me</button> </div> ); } export default App;
-
Run the React Frontend
npm start
This will serve your React app at http://localhost:3000.
-
Navigate back to the my-app folder and create a backend directory for the Go server:
cd .. mkdir backend cd backend
-
Initialize the Go module:
go mod init my-app/backend
-
Create file and Write the Go Backend Code:
touch main.go
Open the file and write the following code:
package main import ( "fmt" "log" "net/http" ) func handleRequest(w http.ResponseWriter, r *http.Request) { fmt.Println("Button clicked, request received!") w.Write([]byte("Backend response: Success!")) } func main() { http.HandleFunc("/api", handleRequest) fmt.Println("Backend running at http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) }
-
Run the Go Backend
go run main.go
This will start your Go backend at http://localhost:8080.
Click the "Click Me" button. If successful, you should see the message “Button clicked, request received!” printed in your terminal where the Go server is running.
If you see any cors issues you can do the following:
-
In your Go backend directory, run:
go get github.com/rs/cors
-
Update Your Go Code in main.go:
package main import ( "fmt" "log" "net/http" "github.com/rs/cors" ) func handleRequest(w http.ResponseWriter, r *http.Request) { fmt.Println("Button clicked, request received!") w.Write([]byte("Backend response: Success!")) } func main() { mux := http.NewServeMux() mux.HandleFunc("/api", handleRequest) // Enable CORS for all origins c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowCredentials: true, }) handler := c.Handler(mux) fmt.Println("Backend running at http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", handler)) }
-
Run again:
go run main.go
Hopefully, this will resolve those errors.