Javascript 我能';我不能让golang用参数识别我的get请求
我正在尝试使用React to work with Go中的参数创建一个简单的axios get请求。无论我做什么,都要不断地获取GET URL path not found(404)错误。 这是JSJavascript 我能';我不能让golang用参数识别我的get请求,javascript,reactjs,go,Javascript,Reactjs,Go,我正在尝试使用React to work with Go中的参数创建一个简单的axios get请求。无论我做什么,都要不断地获取GET URL path not found(404)错误。 这是JS import React, {Component} from 'react' import axios from "axios" class ShowLoc extends Component { constructor(props){ super(props)
import React, {Component} from 'react'
import axios from "axios"
class ShowLoc extends Component {
constructor(props){
super(props)
}
componentDidMount(){
const {id} = this.props.match.params
axios.get(`/loc/${id}`)
}
render() {
return(
<div>
Specific Location
</div>
)
}
}
export default ShowLoc
因为找不到get请求,所以我从未点击过getLoc函数。如何从get请求中获取参数?您尚未使用mux路由器。由于您将
nil
传递给ListenAndServe
,因此它使用的是默认路由器,它连接了所有其他处理程序。相反,向mux路由器注册所有处理程序,然后将其作为第二个参数传递给ListenAndServe
func main() {
r := mux.NewRouter()
fs := http.FileServer(http.Dir("static"))
r.Handle("/", fs)
bs := http.FileServer(http.Dir("public"))
r.Handle("/public/", http.StripPrefix("/public/", bs))
r.HandleFunc("/show", show)
r.HandleFunc("/db", getDB)
r.HandleFunc("/loc/{id}", getLoc)
log.Println("Listening 3000")
if err := http.ListenAndServe(":3000", r); err != nil {
panic(err)
}
}
func getLoc(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
id := r
fmt.Println("URL")
fmt.Println(id)
}
问题是,如果我这样做,那么我的公共目录中的bundle.js文件现在就找不到了。我试着用go后端来做反应。我需要能够通过静态html和JavaScript文件向“/”web地址提供服务,这似乎离不开http默认路由。我不知道anxios库,但不应该设置服务器的端口和地址?
func main() {
r := mux.NewRouter()
fs := http.FileServer(http.Dir("static"))
r.Handle("/", fs)
bs := http.FileServer(http.Dir("public"))
r.Handle("/public/", http.StripPrefix("/public/", bs))
r.HandleFunc("/show", show)
r.HandleFunc("/db", getDB)
r.HandleFunc("/loc/{id}", getLoc)
log.Println("Listening 3000")
if err := http.ListenAndServe(":3000", r); err != nil {
panic(err)
}
}
func getLoc(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
id := r
fmt.Println("URL")
fmt.Println(id)
}