I used Go to create a very simple web app wich only one web page. In this project I used Vue CLI to install webpack. If I run vue application by npm, all is fine and work perfect, but I want get ability work with vue when i run go web app. For this I builded vue-project by npm 'npm run build' and get dist directory, after that in go application I wrote something like this:
func main() {
http.Handle("/static/", http.FileServer(http.Dir("web-vue/dist")))
http.HandleFunc("/", HomePage)
http.ListenAndServe(":8080", nil)
}
func HomePage(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("web-vue/dist/index.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w,"SerGorn")
if err != nil {
log.Print("template executing error: ", err)
}
}
It's work fine with one exception, when I change .vue file, for applying this changes, needs rebuild app by npm again. How I could solve this problem? Any Idea? Or may be it's wrong way and better if work with vue will be in only node.js?
You can use vue with any backend. Seems like FileServer or your browser caches your static files. You could use Cache-Control: no-cache
header to avoid caching.
The .vue source files must be processed in order to be served as HTML by a web server. You cannot modify .vue files and expect to see the changes in a web browser running against a production web server. As such, after modifications you must run npm run build
which will process the .vue files and generate valid HTML files into /dist
.
Now reviewing your code, I see that you could further simplify it by doing this:
func main() {
http.Handle("/static", http.FileServer(http.Dir("web-vue/dist/")))
http.Handle("/", http.FileServer(http.Dir("web-vue/dist/")))
http.ListenAndServe(":8080", nil)
}