Local server http communication and angular browser rendering - node.js

I think I'm doing something completely the wrong way.
I have an Nodejs server running that read in a DB and serve with express some data via http locally (it has to only be accessed locally). It sends the data on localhost on some port (8080 for example). Then I have an angular app on the server that get these datas from an http request on localhost:8080 and display them. The angular app runs locally on localhost:4200.
I was building the entire stuff on my computer and that was working perfectly (I have no problem with CORS). Then I deployed it on a server, and I accessed it via ssh port forwarding. Basically I forward localhost:4200 on the server via ssh on my local computer on localhost:8090.
And my problem is that, when loading and executing the angular app in my browser via port redirection, it's doing a get request to localhost:8080. So it's trying to communicate with the localhost it's running on, which is the client itself.
If you understood my spaghetti situation, there is actually a dirty solution : redirect localhost:8080 on the server to localhost:8080 on the client.
Is there any way to do the get request server side and not in the client's browser so that localhost correspond to the server? Is there a better way to do what I'm trying to do?
I can sum up by : How can you access another local service on localhost on the server with angular app since it executes in the client browser and localhost will refer to client localhost.

Try to use any web server (such as nginx or apache2 or etc.) in your server and make use of proxy and reverse proxy with your node application, it will work
angular2-router-and-express-integration

Related

How to get server IP-address in an Angular application?

How to get the server host IP-address in Angular app started with ng serve --host 0.0.0.0?
The IP-Adress will be used when communicate with the backend server.
Since each coworker has its own IP-Address I should like to avoid to have it hardcoded in the code. I can´t use localhost either since then my coworkers can´t access it.
Is angular running completely on client side or is it possible to add something to fetch the IP-Address when the application get the first request?

Making an http request from react app with API running on localhost

On my main PC I have a react app running http://localhost:3000, aswell as a nodejs and express API http://localhost:3001, so my react app requests the data from the API that is running on the same machine and it works fine, it loads up the web app and succesfully makes the http requests. I now want to visit the react app from a different computer, so when I vist my react app, using my main computers internal IP, the react app loads up perfectly fine, but the API request fails. In console I get GET http://localhost:3001/today net::ERR_CONNECTION_REFUSED, when making my axios request I dont refer to the API as http://localhost:3001 I use the public IP of my computer instead of localhost, I also have went into my router and forwarded ports 3000 and 3001. So it looks to me like even when I'm on a different computer, the react app still thinks it is on localhost. How can I fix this ?
When I port forwarded ports 3000 and 3001, I forgot to change the protocols from tcp to tcp/udp. It works fine when I use both TCP and UDP to port forward
I think you are going CORS (cross-origin resource sharing ) issue. I mean you have this error because your server (NodeJS) an your App(React) are running on the same computer. So you need to enable CORS :
open your package.json file and add this property :
"proxy":"http://localhost:3001"
2)In your React App instead of making your requests to fetch("http://localhost:3001/users") you'll send your requests like this fetch("/users")
Click here to read more

Deploy Create-React-App on a remote Windows Server

I am an inexperienced intern working with a remote Windows Server and React. The Windows Server is running in the company network. I have created a dynamic React website with a NodeJs backend and React Router. I have only ran it on the localhost development server. I want to try to deploy it on the remote Windows Server and give it a custom domain name (Something which can be accessed like servername/myreactapp/).
So far, I have had no success trying to make it work with IIS, even with a web.config file (I get 404 and 500 errors). I am currently making it work by actually running the development server and the nodejs server in the Windows Server, and I access it through the server IP at port 3000.
An improvement would be to be able to access the port through the server name (like servername:3000, instead of the server_ip:3000), but ideally I want to be able to access it like servername/myreactapp/.
Any help would be appreciated. Thank you very much.
The simple solution would be to run your app on port 80 then you will not have to specify the port number.
The best solution would be to set up Nginx on the server and proxy_pass / route to port 3000.
If its running on localhost, which would be port 80, the url would be like http://your_server_name:80, and would be accessible by anyone on the same network, as long as your authentication allows it.

axios.post gives error on my VPS when sending post request locally to another app. Everything works fine on my local computer

So I have a Nginx configured to forward requests trough port 433 (SSL) to localhost:3000 where react is running, react shall in turn post requests to a Node.js app running on the same VPS (localhost:8080) to make a stripe payment.
Everything work fine so far. The frontend is rendering, SSL working.
But when the i run axios.post with the payment data from React it never reaches the Node (as far as my logs tell me).
I have CORS setup in node to accept a bunch of "localhost" variants and the actual domain itself.
Why is this not working?
Thanks!
I should have been poitning the React app to the Url of the api, not its local adress on my VPS sorry.

Node express from webserver

I have been developing a standalone app for an event exhibition. This comprised of a backbonejs frontend with a node express server backend for saving data. At the event this will run over localhost fine but how can I make the express server be accessed via normal http. I.e the backend responds when app is added to my webserver for client review
Any ideas.
G
Servers available at localhost should respond to http requests if your firewall is open. If you are behind a router, you need to configure it to forward requests to your machine. NAT is the simplest (and least safe) way.

Resources