This is a basic blogging api, with golang backend, sqlite and react frontend. There are a lot of redundant codes, and bad software practices such as server.go contains code for sqlite as well which is not proper. But yeah I was learning when I wrote it. I am aware of the fact that react/babel is in development build.
- Install go from Golang, Follow instruction properly.
- cd react-front-go-backend/backend
- go run server.go
- The front-end is an experimental build, so you need not install anything.
- cd react-front-go-backend/frontend
- Run index.html on a browser.
- w.WriteHeader(310), it is used to return custom response.
- w.Header().Set("Content-Type", "application/json"), is used to edit default http headers.
- if you map your request string of json to golang int or vice versa you will get errors containing this keyword "unmarshal"
- "_" is used when you don't want to do anything with the variable but the function is returning it. So in golang you have to define variables if function is returning two values but if you are not going to use it then you code will give errors.
- In struct value of parameters should start with capital or you might get undecipherable errors.
- Ref
- If you want to change DOM the only way you can add is through return in render nothing else.
- If you try to add jsx somewhere else, you will fail badly.
data1={
Name: this.state.blogid,
Description: "like"
};
console.log(data1)
fetch('http://localhost:8080/like', {
method: 'post',
headers: {
"Content-Type": "application/json"
}
body: JSON.stringify(data1)
})
- This is how you do post method properly.
- If you want to change anything in DOM you have to add code like
<p class="content">{this.state.content.Content}</p>
- Now whenever any event happens, state changes then this will definitely change.
- If you want to add conditional rendering you have to do it in render function.
fetch('http://localhost:8080/blog/'+name)
.then(response => response.json())
.then(json => {
console.log(json)
this.setState({content: json})
this.setState({likebutton: 1})
this.setState({blogid: name})
console.log("hi there "+this.state.content.Title)
})
- this is important as fetch passes the return value to then and is passed as response, and response.json() is passed as json in .then()
- Whie doing a post or get request using javasvript then, always add "http://localhost:8080" rather than "localhost:8080" to avoid browser CORS error.