duanqianpu6499 2019-09-17 14:28
浏览 66

在Main.go中连接ReactJS文件

I am trying to develop a web application with ReactJS and a Go server. The web app works when I connect ReactJS file with Node.js, but it shows an error on the output when I connect ReactJS file with the Go server.

output:

"404 page not found"

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)
}

my file structure:

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

I would like the web app to load App.js when I run main.go

What am I missing?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试