Answer the question
In order to leave comments, you need to log in
How can I configure the server to run a Golang backend and an Angular2 frontend?
Good afternoon! I want to write a web application whose data will be processed by the Golang backend and the pages will be rendered by the Angular2 frontend. Moreover, the server will be written in restful and will not render html at all. I need this so that later it would be easy to access the existing api from mobile applications. The fact is that angular2 must have its own server running, which deals with routing and returning statics, but my go server must also be running. And the question is, how can I run all this together on the same host, so that Angular and Go work together? I heard that it is possible to run all this with nginx, but I have no idea how to do it. Thank you.
Answer the question
In order to leave comments, you need to log in
Maybe I don’t understand something, but Angular works on the client, and it only needs to get all its files from the server and communicate with the backend api.
you can configure nginx
like this:
server{
listen 80;
server_name example.com;
location /api{
proxy_pass ВАШ БЭКЕНД
}
location / {
# отдача статики для ангуляра
try_files $uri /$uri 404;
}
}
Is it really impossible to do without the "Angular server"? Somehow not rational.
nginx allows you to give part of the URL to one, and part to another.
Use different locations:
location .... {
}
Hiding the go server behind nginx is not the best idea. There will be drawdowns on a highly loaded project.
First, decide on a router for the go server, and then you can ask questions about how to tie it to Angular.
In addition to my comment below :
func main() {
http.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "/var/www/default/htdocs/index.html")
})
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("/var/www/default/htdocs/public"))))
http.HandleFunc("/json", func(response http.ResponseWriter, request *http.Request) {
// serves a JSON response
})
http.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
// replies to the WebSocket
})
http.ListenAndServe(":3000", nil)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question