user3070246
user3070246

Reputation:

How to send data between two different port on one application

I'm using expressjs library. It works on localhost:8000 as a server and i'm showing static pages with it. My application works on localhost:4200. I tried to use same cookie or localStorage on different pages of ports but it didn't work. How can i achieve this?

Upvotes: 0

Views: 1611

Answers (3)

Cypherjac
Cypherjac

Reputation: 879

When you're working on different ports, if the cookies have the 'SameSite' attribute set to 'Host Only' and the domain set to 'localhost', they will be visible/accessible from both domains regardless of the port number.

As @codingmaster says, if it's different domains, you will have to implement cors management for different domains..

But since you own both applications, during production, one can be on the main domain while the other on a subdomain, then you'll have to set the domain attribute of the cookie to your domain, e.g: example.com and the SameSite attribute can remain HostOnly and the cookies will be accessible

Upvotes: 0

Satya Prakash Satyam
Satya Prakash Satyam

Reputation: 27

You can do this by calling the routes on localhost:8000 in your main application only, and since it is working on two different ports you may run to error due to Cors policy and for that you may look up to these answers. cors enable

Upvotes: 1

codingmaster398
codingmaster398

Reputation: 257

What I think you are trying to do here is have the cookie/local storage shared between two locations.

  1. Do you need 2 locations? I'm pretty sure you can serve your app on the same express server.
  2. If you are using the express one for backend (not frontend), you can pass the required information within your requests, with POST, GET, PATCH, etc. Have a google for more info on sending info between a client and server.
  3. If both are frontend, maybe try to not have it that way. If it's necessary, you can use what I mentioned above to send info between the servers.

Hope this helps in some way.

Upvotes: 0

Related Questions