How to setup apache2 virtual host with 2 domains (same domain) for SPA application?

I’m developing 2 sites as frontend (http://localhost:2000) and backend site (http://localhost:5000). I try to send cookie from frontend to backend but not success and I know to do this I must set 2 sites is the same domain.
The first thing I want to say is I dont know “the same domain is” and here is what I try. I make a config file like this:

<VirtualHost *:80>
     ProxyPreserveHost On
     ServerName fe.ecom-dev.local
     ProxyPass / http://fe.ecom-dev.local
     ProxyPassReverse / http://fe.ecom-dev.local/
</VirtualHost>
<VirtualHost *:80>
     ProxyPreserveHost On
     ServerName be.ecom-dev.local
     ProxyPass / http://be.ecom-dev.local
     ProxyPassReverse / http://be.ecom-dev.local/
</VirtualHost>

and in the hosts file:

localhost:2000	fe.ecom-dev.local
localhost:5000	be.ecom-dev.local

What did I do are correct?

First, the hosts file doesn’t understand (nor should it) port numbers, it should be changed to just

localhost	fe.ecom-dev.local
localhost	be.ecom-dev.local

As for your cookie problem, you should indicate to the browser that the cookie is for ecom-dev.local, rather than be.ecom-dev.local or fe.ecom-dev.local. How you do this depends on the technology you’re using, but generally there is a parameter called domain when you set a cookie, that should be ecom-dev.local.

For a javascript example, see https://javascript.info/cookie#domain