首页 > 解决方案 > 在 Main.go 中连接 ReactJS 文件

问题描述

我正在尝试使用 ReactJS 和 Go 服务器开发一个 Web 应用程序。当我将 ReactJS 文件与 Node.js 连接时,Web 应用程序可以工作,但当我将 ReactJS 文件与 Go 服务器连接时,它会在输出中显示错误。

输出:

“404页面不存在”

main.go:

package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

var items = []*Item{}

// Item representing the todo
type Item struct {
    ID   string `json:"item_id"`
    Name string `json:"item_name"`
}

func main() {
    //r := http.NewServeMux()
     r := mux.NewRouter()

    r.PathPrefix("client/").Handler(http.StripPrefix("client/", http.FileServer(http.Dir(".client/"))))
    r.HandleFunc("/api/new-item", addItemHandler)
    fmt.Println("Running server on port :3000")
    http.ListenAndServe(":3000", r)
}

func addItemHandler(w http.ResponseWriter, r *http.Request) {
    item := &Item{}
    json.NewDecoder(r.Body).Decode(item)
    items = append(items, item)
}

我的文件结构:

appName/main.go
appName/client/src/App.js

我希望App.js在运行时加载网络应用程序main.go

我错过了什么?

标签: reactjspostgresqlgo

解决方案


推荐阅读