http/template not working with React.js (javascript)

662 views
Skip to first unread message

Yelnatz

unread,
Nov 1, 2013, 1:22:41 PM11/1/13
to golan...@googlegroups.com
How come parsing a template then using it won't work with react.js?
If I serve the file directly, it seems to render alright.

How can I make this work?

Using template:

package main
import (
    "net/http"
        "html/template"
)
var t, _ = template.ParseFiles("index.html")
 
func handler(w http.ResponseWriter, r *http.Request) {
    t.Execute(w, nil)
}
 
func main() {
   http.HandleFunc("/", handler)
   http.ListenAndServe(":8100", nil)
}
 

Non-template:

import (
    "net/http"
)
func main() {
    http.ListenAndServe(":8200", http.FileServer(http.Dir(".")))
}


Cheers.

Attached are relevant files for convenience.
index.html
react.js
server.go
server2.go
base.css

Donovan Hide

unread,
Nov 1, 2013, 1:36:46 PM11/1/13
to Yelnatz, golang-nuts
In the first example you are serving the same file for all the urls:

/
/base.css
/react.js

Have a look in the developer tools in Chrome or use curl to the test the responses. You need to do some routing, so that / is handled by the template handler and the static files are handled by http.FileServer. It usually helps to put all the static files in a public sub folder.

If all this is a bit new to you, might be worth having a look at one of the web frameworks, such as gorilla/mux or revel:


Hope that helps,
Donovan.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Yelnatz

unread,
Nov 1, 2013, 1:51:29 PM11/1/13
to golan...@googlegroups.com, Yelnatz
So what's wrong is that when a browser GETs the files needed for the html (/base.css /react.js), my go server replies with the index.html again?

If it is, thanks. 

I looked into it more and found this:

Thanks again.

Specode

unread,
Nov 4, 2013, 3:32:31 AM11/4/13
to golan...@googlegroups.com
try:
http://play.golang.org/p/2-vy0ntEGo

then edit index.html:
<script src="http://example.com:8100/js/react.js"></script>
<link rel="stylesheet" href="http://example.com:8100/css/base.css">
Reply all
Reply to author
Forward
0 new messages