0

I'm creating a web project where I am intergrating both Angular, for the frontend, and Go for the backend. With the backend I need to use the HTTP web server in order to display data using a RESTAPI, however, my issue is that I cannot run both servers on port 80 (which is basic knowledge). But I was wondering if there was a work around to this. I prefix all my backend pages with the prefix /backend/ (so for example https://example.com/backend/auth), therefore there shouldn't be any file/page collisions taking place.

One of the main reasons I am trying to accomplish this is because I want to set the cookies for the site in the backend as it'd be far easier and less complex for my situation, but I'm not sure how to because unless they are on the same port they would not share the same cookies.

Any help would be greatly appreciated, thank you.

Nicster15
  • 159
  • 3
  • 12
  • I would have goServer listen on a different port and create a rule that will route the domain you are showing to that port. – Judson Terrell Aug 21 '17 at 01:01
  • @JudsonTerrell Issue is cookies won't be shared. Else I would just make the go server on a domain like backend.example.com. – Nicster15 Aug 21 '17 at 01:14
  • Man that's tricky then. Every application action I've ever built had to run the web services on a different port. However if your go server is serving the static files for the angular app, you should be able to share the same port. – Judson Terrell Aug 21 '17 at 01:27
  • @JudsonTerrell Would you mind showing me an example? I tried that and it seemed to just give me a blank white page. – Nicster15 Aug 21 '17 at 01:40
  • Why are using cookies as opposed to headers etc – Judson Terrell Aug 21 '17 at 01:42
  • I'm mobile right now but I can try tomorrow – Judson Terrell Aug 21 '17 at 01:42
  • @JudsonTerrell Well what I'm doing (and it may be unorthodox because I've never done this whole frontend communicating to backend over different web servers -- so if it is please tell me so and what I should do instead) is pretty much when the angular page is loaded it sends a get request to the backend auth page, returning values including if the user is logged in, their username, etc. It gets all this information by reading the cookie on the website, grabbing the token key (randomly generated) along with some other stuff and grabbing the needed information from the database. – Nicster15 Aug 21 '17 at 01:45
  • You definitely need to look into using headers and session storage. I will explain more in the morning :-) also it wouldn't be a bad idea to use node server – Judson Terrell Aug 21 '17 at 01:55
  • @JudsonTerrell Just tried out static files, it seems I just receive a empty white page though rather than seeing the actual html/css/typescript content. – Nicster15 Aug 21 '17 at 01:55
  • You need a URL rewriter to send all static requests to index.html – Judson Terrell Aug 21 '17 at 01:56
  • What is "Go" exactly? Can you send me a link to docs? – Judson Terrell Aug 21 '17 at 01:59
  • @JudsonTerrell I did the following: router.Use(static.Serve("/", static.LocalFile("./../frontend/src", true))) Inside of the src directory is where the index.html is (and I've confirmed this path is correct). – Nicster15 Aug 21 '17 at 01:59
  • Don't you need app.use? Router is for API requests – Judson Terrell Aug 21 '17 at 02:00
  • @JudsonTerrell I'm not using Node which you may think, I'm using Golang – Nicster15 Aug 21 '17 at 02:04
  • I see. I can only help you conceptually then :-( – Judson Terrell Aug 21 '17 at 02:05
  • I know that in node if I serve static file from the same server instance that the APis live in, I can do what your asking, and generally for port 80 all web assets are static so even if you have theoretically a server that can run the project locally once it gets deployed the server would have to run on a different port. You could look into using Cors or something maybe and run the server under a sub domain. – Judson Terrell Aug 21 '17 at 02:14
  • https://medium.com/@thanhngvpt/golang-and-angular-2-why-not-38a398b182c, use the router to serve static files which should load your angular app. – Alex Efimov Aug 21 '17 at 07:36

1 Answers1

0
  1. If both frontend and backend are on same server (but different ports), will they share cookies?
    Yes, they will, check this SO answer. As per latest RFC, cookies for a given host are shared across all the ports on that host.

  2. How to verify the user account from frontend to backend?
    a) If using social authentication, check this SO question, and this great article. In short, your frontend gets the access token from OAuth and passes it to backend which can call the OAuth provider (e.g. Graph API) to verify the access token. Create a new session and pass to frontend.

    b) If NOT using social authentication, your users will enter their username/password which the frontend will receive. It can pass those credentials to backend, which will then generate a session and pass to frontend.

    c) In both a) and b), the frontend is creating cookie with session information given by backend. On every user request frontend would receive the cookie, hence the frontend would have to validate session with backend on every user request.

    d) A better way is to replace cookies with JWT Tokens but the flow would remain the same.
Rash
  • 7,677
  • 1
  • 53
  • 74