前提・実現したいこと
実現したい事は、
React(axios)からapi化したGoにagoutiのキック
↓
agoutiでheadlessでスクレイピングを行い対象の情報を取得
↓
axiosで帰ってきた文字列を取得
です。
最後の「axiosで帰ってきた文字列を取得」が上手くいきません。
適当に作ったプロジェクトでmain.goのみを動かしてみましたが、
そこでは上手く動作したのでagouti自体は動くプログラムになっていると思います。
axiosからgoへのキックが上手くいっていない?ように見えるのですが。。
環境はDocker上で各種展開、
Webサーバーはnginxです。
何か分かる方がいれば宜しくお願いします。
App.js
import React from 'react'; import axios from 'axios'; class App extends React.Component { handleSubmit(event) { axios.get('/api') .then((response) => { console.log(response); }) .catch(console.error); } handleSubmit2(event) { axios.get('/api/py') .then((response) => { console.log(response); }) .catch(console.error); } handleSubmit3(event) { axios.get('/api/ls') .then((response) => { console.log(response); }) .catch(console.error); } render() { return ( <div> <input type="button" value="Submit" onClick={this.handleSubmit}/> <input type="button" value="Submit2" onClick={this.handleSubmit2}/> <input type="button" value="Submit3" onClick={this.handleSubmit3}/> </div> ) } } export default App;
main.go
1package main 2 3import ( 4 "fmt" 5 "net/http" 6 "os" 7 "os/exec" 8 9 "github.com/sclevine/agouti" 10) 11 12func main() { 13 14 http.HandleFunc("/", echoHello) 15 16 http.HandleFunc("/py", echoHello2) 17 18 http.HandleFunc("/ls", echoHello3) 19 // port 20 http.ListenAndServe(":8000", nil) 21} 22 23func echoHello(w http.ResponseWriter, r *http.Request) { 24 fmt.Fprintf(w, "<h1>Hello World</h1>") 25} 26 27func echoHello2(w http.ResponseWriter, r *http.Request) { 28 29 options := agouti.ChromeOptions( 30 "args", []string{ 31 "--headless", 32 "--disable-gpu", 33 }) 34 35 driver := agouti.ChromeDriver(options) 36 defer driver.Stop() 37 driver.Start() 38 39 page, err := driver.NewPage() 40 if err != nil { 41 fmt.Fprintf(os.Stderr, "%s\n", err) 42 return 43 } 44 45 page.Navigate("https://qiita.com/") 46 47 title, err := page.Title() 48 if err != nil { 49 fmt.Print(err.Error()) 50 } 51 52 fmt.Fprintf(w, title) 53} 54 55func echoHello3(w http.ResponseWriter, r *http.Request) { 56 57 output, err := exec.Command("ls").Output() 58 if err != nil { 59 fmt.Print(err.Error()) 60 } 61 62 fmt.Fprintf(w, string(output)) 63} 64
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。