1
0

More than 1 year has passed since last update.

Go の API をフロントサイドから呼び出す

Posted at

API 側に

http.Handle("/", http.FileServer(http.Dir("static")))

"static" フォルダに html などを入れる。

詳しく

main.go に

http.Handle("/", http.FileServer(http.Dir("static")))
log.Fatal(http.ListenAndServe(":8080", nil))

と書いた場合。(main.go があるフォルダ)/static に index.html を入れる。

go run main.go

などで実行すると、localhost:8080/ で index.html が表示される。
localhost:8080/test.html とかで static/test.html が表示される。

"static" フォルダは別フォルダにしても動く。"../test" とかでも問題ない。
"/" を "/top" にして localhost:8080/top にアクセスすると not found になったので、"/" は変えられなさそう。

題名と書いてること違うじゃないのか?

はい、そう思います。
ただ、自分は上記のことがしたかったものの、フロントサイドを別 port で立てて cors を設定し、、、などをやっていて、かなり詰まりました。そういう人の検索に引っかかりやすい題名にしたつもりです。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0