Angular 2/4 connecting to Golang

Hi,

I am very new to golang and web development I went through http concepts and also completed go tour. I have a good knowledge on Angular 2 and thus created a simple login page front-end however I am finding difficulty connecting it to golang

package main

import (
	"log"
	"net/http"
)

func main() {
	fs := http.FileServer(http.Dir("./client/dist"))
	http.Handle("/", fs)

	log.Println("Listening...")
	http.ListenAndServe(":3000", nil)
}

I tried using above code to connect to my client and atleast handle it but it gives me error Uncaught SyntaxError: Unexpected token < … also I dont understand how I will target inputs from the angular forms to get data from there so that i can authenticate it on my application…

It looks like the directory client/dist contains files you want to server over HTTP. What are these files? JSON?

Which URL does your Angular application request? What is the response to this request? What does Angular expect it to be?

1 Like

Try this, if I recall correctly index.html request for some js files but the router doesn’t point to them correctly.

1 Like

Hi Atharva,
Welcome here.

The “Uncaught SyntaxError: Unexpected token” is a Javascript error, do you get this error when you serve the app with Angular’s dev tools as well?

I just want to make sure I understand what you are trying to do. Do you simply want a Go server to serve your Angular app, or do you want Go to handle the HTTP requests coming in from the Angular app?

Pieter

Did our suggestions work?

yes I simply wanted to serve my angular code via golang also … I wanted to know like normally we make different html files and target them for inputs and outputs via golang will the same concept work in angular as well … if i put those codes in different components of angular and try to fetch data via a form.

and no I didnt have any error when running the app normally

@dyxj well i dont understand gorrilla thing i am trying to learn things without any framework and i wrote an equivalent code there for it which should work but i dont know for some reason it doesnt actually this worked for me once beofore

Sorry it took me too much time to respond i was on vacation …

Hmm I think “Uncaught SyntaxError: Unexpected token” is too vague to pin point the problem. Maybe more info such as :

  • in which file/line it occurs as shown in.
  • you could also try printing the request URI in go to find out what is going on.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.