Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I setup ngrok for front-end and back-end at the same time?

I want to setup ngrok for front-end as well as back-end at the same time which is not possible by the way because my front-end is on Angular 6 and back-end is in the .net core.

I implemented ngrok when both front-end and back-end were on the same host and port. (Both was in .Net and MVC). So they were running on the same port.

Now, I want to know if there any alternative way to do this.

My Angular 6 runs on http://localhost:4200/ Back-end runs on https://localhost:44343/

Please Suggest me if any.

like image 964
Karan Patokar Avatar asked Feb 07 '19 06:02

Karan Patokar


People also ask

Can I run frontend and backend on same port?

You'd want to somehow combine both servers, so they can interact with each other. But each can only run on its own port. You can't start both on the same one and if they are apart, it's impossible for the frontend code to access the backend endpoints.

Can Ngrok be used for production?

NGROK is a great tool, but when you are moving to a production environment there are some things to be aware of. Without a license, the NGROK URL will expire after a short period. A single simple tunnel is a single point of failure and can work against you if not managed properly.

How can I make Ngrok faster?

Luckily, ngrok has servers in many regions and using a server located closer to your geographical location means lower round trip time and faster connection speed. You can change the location of ngrok server using the -region flag.


2 Answers

I've found a solution using tunnels for both back and front end, defining them in the ngrok.yml config file. This file in Windows is usually found on C:\Users\you\ .ngrok2\ngrok.yml.

authtoken: <your token is already here>
tunnels:
    frontend:
        proto: http
        addr: 3000
    backend:
        proto: http
        addr: 1337

Where my ports are 3000 for React.js and 1337 for Sails.js.

To lift the ngrok service: ngrok start frontend backend

Ngrok will redirect two ports and provide two addresses instead of one (four in total, if you consider https).

Finally, I've changed the API address on the frontend using the new URL found in the ngrok CLI (you will identify it by looking at the port number).

I came to that solution reading a similar post (you can find details here) and not using the "subdomain" part (which is available only in ngrok paid service).

like image 108
Moisés Baddini Avatar answered Sep 30 '22 04:09

Moisés Baddini


Good question! I have a similar problem and I made my backend locally behave like a proxy server for frontend paths so it routes the incoming frontend requests to the local frontend server. In Python, it was easy with Flask, using the proxy method described here: https://stackoverflow.com/a/36601467/38611

like image 43
jbasko Avatar answered Sep 30 '22 02:09

jbasko