我正在尝试创建一个go后端,该后端将为我的react前端服务(从go服务器托管React文件,而不仅仅是在后台可以向我发出请求的go服务器)。
这是我编写的GitHub代码。
https://github.com/mip2115/values
我希望能够在输入(App.js)中写一些东西,并通过使用server.go中PostForm函数中的FormValue()函数在go后端中检索该值,而不必通过JSON对象。
我不确定要执行此操作需要更改什么,但是我认为我需要捆绑javascript并指向index.html中的捆绑文件。 有人可以告诉我我需要对代码进行哪些更改吗? 奖励,如果您能告诉我如何添加热编译。
server.go文件是我的go服务器所在的位置。
下面是相关代码:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>React Starter</title>
</head>
<body>
<div id="root"></div>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<script src="../dist/bundle.js"></script> <!--references the react app -->
</body>
</html>
app.js
import React, { Component} from "react";
class App extends Component{
constructor() {
super();
this.state = {
query: '',
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
query: event.target.value
});
}
render() {
return(
<div className="App">
<h1> Hello, World! </h1>
<form action="/api/postform" method="POST">
<input type="text" name="query" onChange={this.handleChange} />
<input type="submit" value="Submit" />
</form>
</div>
);
}
}
export default App;
webpack.config.js
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
options: { presets: ["@babel/env"] }
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
resolve: { extensions: ["*", ".js", ".jsx"] },
output: {
path: path.resolve(__dirname, "dist/"),
publicPath: "/dist/",
filename: "bundle.js"
},
devServer: {
contentBase: path.join(__dirname, "/"),
port: 3000,
publicPath: "http://localhost:3000/dist/",
hotOnly: true
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
package.json
{
"name": "r-proj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --mode development"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"css-loader": "^1.0.0",
"style-loader": "^0.23.0",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.8"
},
"dependencies": {
"react": "^16.5.2",
"react-dom": "^16.5.2"
}
}
server.go
package main
import (
"net/http"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"fmt"
)
func main() {
// Set the router as the default one shipped with Gin
router := gin.Default()
router.Use(static.Serve("/", static.LocalFile("./index.html", true)))
// Setup route group for the API
api := router.Group("/api")
{
api.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H {
"message": "pong",
})
})
}
// input form should hit this function when it posts
api.POST("/postform", PostForm)
router.Run(":5000")
}
// should be able to retrieve the value netered into the form and print it out.
func PostForm(c *gin.Context) {
c.Header("Content-Type", "application/json")
q := c.Request.FormValue("query")
fmt.Println("VALUE IS: ", q)
}