0

I am new to this forum and a newbie in Angular2 and Golang. My issue is that I would like to try out a template on a Go server.

So, I created a main.go file containing this main() function :

func main() {
    r := mux.NewRouter()
    p := http.StripPrefix("/", http.FileServer(http.Dir("./src/")))
    n := http.StripPrefix("/config", http.FileServer(http.Dir("./config/")))
    r.PathPrefix("/config/").Handler(n)
    r.PathPrefix("/").Handler(p)
    fmt.Printf("Starting server on :33166 \n")
    http.ListenAndServe(":33166", r)
}

and run go run main.go on the project directory, but its fails in this request

GET http://127.0.0.1:33166/webpack-dev-server.js

It might be very simple, but I couldnot figure it out. So, any idea to how I'm able run the project on server would be appreciated. Thanks.

jiji
  • 337
  • 2
  • 3
  • 13
  • Hey I would recommend adding the webpack tag if there is one. Your index.html is trying to find the dev-server file. https://webpack.github.io/docs/webpack-dev-server.html explains the function but basically you should be installing this as a npm install. Have you ran npm and bower installs? Also NPM has an option to run a server which will use express to host the file. Maybe you know that and want to run it with go is that the case? – PatrickWalker Aug 04 '16 at 10:41
  • Yes, indeed I want to run the project on go server, not node server. I didn't understand what you said about the tag. doing `sudo npm install --global webpack webpack-dev-server` in the command line correctly installed it but still the same probleme as above I don't know what else I can do. Thank you for your response – jiji Aug 04 '16 at 12:33

1 Answers1

1

According to the ng2-admin installation guidelines, if you want to use ng2-admin with another backend (golang in your case), you need a release build using the following command:

npm run prebuild:prod && npm run build:prod

From installation guidelines:

This will clear up your dist folder (where release files are located), generate release build (...). Now you can copy the sources from a dist folder and use it with any backend framework or simply put it under some web server.

EDIT: Thanks to @PatrickWalker for his comments.

jnmoal
  • 985
  • 5
  • 7
  • Thanks for the reponse. I tried but it failed because it **Cannot find module 'webpack-merge'** – jiji Aug 04 '16 at 13:02
  • npm run build:prod failed because there is no 'webpack-merge' modules and npm run server:prod failed at http-server dist --cors because http-server: not found – jiji Aug 04 '16 at 13:18
  • Did you run npm install first? – PatrickWalker Aug 04 '16 at 13:32
  • Like said in the installation guidelines, you first need to run npm install if you haven't, and then bower install if there is any issue. So try to run both npm install and bower install before using the command. – jnmoal Aug 04 '16 at 13:45
  • 1
    Yeah I ran the installs and the above commands worked. I wouldnt run the server:prod bit if you're goal is to host it in gorilla mux based go server. I ran the build then copied the dist folder to the folder you had your go code in. Updated it to post from /src to /dist and it worked but Jeans answer above gets you there. It;s the build:prod bit you really want – PatrickWalker Aug 04 '16 at 14:06
  • If he hasn't built successfully yet he probably won't need it but it certainly won't hurt as it's clearing the dist folder – PatrickWalker Aug 04 '16 at 14:18
  • Thank you two so much, run the command and did the /src to /dist and it finally worked!! Cheers – jiji Aug 04 '16 at 15:17
  • **Update** : I have a problem, I have to run `npm run prebuild:prod` before `go run main.go` everytime I make changes or else it willl not be updated, and it takes so much time. So it's not the `go-server` who loads the project.. Do you have any suggestions? Thanks – jiji Aug 04 '16 at 16:10
  • Well may be you should use npm start (which will start a devel server) during your development phase. And then, when your ready to go to production, run the commands above to use your golang backend. This way it'll be easier IMHO. – jnmoal Aug 04 '16 at 16:23
  • I unfortunately can't, I need to work with go-server – jiji Aug 04 '16 at 16:51
  • Another solution would be to use some tasks runner, like Gulp or Grunt, which will do all the updates for you. I suggest you post another question, or update this one, so that you can have a clean and proper answer. – jnmoal Aug 04 '16 at 16:53