reactjs - 在 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
我错过了什么?
解决方案
推荐阅读
- python - 有没有办法点击一个通过 GET 请求公开 API 并将该数据流式传输到 kinesis 数据流的 url?
- typescript - 是否支持带有 Vue 3 的 Devextreme 打字稿?
- mysql - Mysql范围查询
- angular - 当我们使用反应式表单时,更改下拉选择字段的最佳方法是什么
- arkit - 将高质量纹理应用于 ARKit 4 网格
- r - 在 RStudio 中从光标到代码块的开头执行
- go - Cognito SAML 服务器端登录
- wget - wget 更改下载的文件扩展名
- json - 如何转换/展平此 Excel 工作表
- javascript - 更改标签内的文本 onclick