Why mongodb on production is using local computer connection? - javascript

I am developing a react-admin site and using mongodb. When I uploaded files to the server, it seems that production version on server is using my local computer mongodb connection. This is my connection string: mongodb://localhost:27017/my-collection
If I try to access a site from my computer, it works, because it uses local computer connection, if I try to access it from phone , it does not works, only shows Could not connect to the server
Site is served by nginx and run with pm2
Can somebody help? What I am doing wrong?

I had to change url in react-admin dataProvider. it had localhost:3001, but I had to changed it to myDomain.com:3001

Related

What does io.connect('http://localhost:4000) mean?

I am building an application using react.js and socket.io, I have my backend code for the socket.io server in one folder and the client/ frontend react.js code in a separate folder. I have it set up to where the server is listening on local host:4000 and the client is listening on 3000, I have connected the front end and back end in my app.js using this line of code..
const socket = io.connect('http://localhost:4000');
I am curious as to what this means. does it mean that when I deploy my website that it will be hosting my server from my computer? Does it mean that it will be hosting the sockets from the client's computers? is the localhost:4000 used for testing purposes and will need to be changed later upon deployment of the website? If none of these are correct, any explanation would be greatly appreciated. If my code is fine the way it is and will not need to be changed upon deployment of my website, please let me know.
Thank you to anyone who can help!!!
does it mean that when I deploy my website that it will be hosting my server from my computer?
It means it will try to connect to a Socket.io server running on the same computer as the browser is running on.
This will usually fail. (Your development environment is an exception because you are running both browser and server on the same computer).
is the localhost:4000 used for testing purposes and will need to be changed later upon deployment of the website?
Yes.
Yes, you will need to change it when you deploy it to your site.
'http://localhost:4000' is an absolute reference meaning it will auto resolve to the localhost environment. It is also advisable to switch to https for a more secure connection.

How to connect frontend and backend on domain

I don't really have experience with backend and how things work together. I have created a simple live message sending app with node.js and socket.io. When I host a static web server on my machine (http-server which runs on local port using node.js) my app works perfectly fine but when I upload it on my host or github pages just for test, the backend doesn't seem to work. I uploaded all my files with an FTP program and the frontend loads fine but the backend doesn't. Do I have to know something like Django or ASP.NET to make these work on my host?
EDIT: One more thing, first line in my server.js is const io = require('socket.io')(3000)and in my script.js - const socket = io('http://localhost:3000')where 3000 and localhost:3000 stands for local host in my machine. What do i need to put instead of these?
You probably need to install and setup Node.js on your server, contact yout hosting provider for node installation if the option isn't available in yout cPanel.

Send get request to local api from react native mobile project

I am developing a mobile app using react native, SQL Server and NodeJS.
I got an API in my localhost. I run my application on an emulator and try to get request from API. But I got this message in catch(error):
[Error:Network Error].
Can somebody help me please.
Actually you are unable to access the local development server because port hasn't been forwarded by ADB yet. Try to replace the "localhost" with your machine IPv4 address and check else you can also try map your local server port just like React Native does.
To forward a port using ADB run the following command in your terminal
adb reverse tcp:8163 tcp:8163 remember this only works if you are using Android 6.0+.

How to give the server port to client

I have a node server with several node projects. I use nginx to get them all responding on port 80. Now, this works for the initial http request. For the websockets, I need to use the direct server port. To keep everything alive while developing I would like to try this, projects will have a dev and live version. Once de dev is stable, I will copy it to the live folder. The live folder is runned by a systemctl script where I define a difrent port to the live version so I can dev without taking the live down. The problem I encounter now is, how can I get the running server port in my client side Javascript so that the dev page connect to the dev port and visa versa?
currently I'm only using express, socket.io and mysql. I have no further npm packages installed. I searched allot but there is not to mush I can find. I found how to connect the socket to the page url but I cannot use that because that URL will always be on port 80. Further I found allot of huge packages that has no use for me since the original page is just static, the dynamics all run over websockets.
Is there any way to parse the port number in the clients .js file like I could do fairly easy in php? And if so, what would be the most efficient way. I could let javascript check if the page uses the live or dev URL but I would prefer not to hardcode my dev URL into JS where it is for everyone to see.
run a third node.js socket.io server program, all your clients connect to this server first.
In this simple node.js program, determine the type of clients by any means. e.g. different user id for dev/production users
send the server url and port to your client according to its type (dev or production)
you may also use this technique to separate your users to different production servers.

Autorun file when you connect to vagrant server

I am currently working on a server where I want to autorun a file whenever I connect to the server myself. Example; I start the server and when I connect to it I want it to run a backup. My server is running on another computer and I connect to it from my main computer to save performance. The thing is that I also want to be able to work inside the server from my main computer. Example; The server is running on PC1 and I want to have access to the server from PC2 via vagrant.
The server is run by vagrant.
I have some experience within - Ruby, C++, Vagrant, javascript and SQL if this is helpful for the answer.
I found how to connect to the server remotely via the vagrant environment using ssh vagrant#192.168.x.x where the x.x is the ip of the local computer.
Example
IP to the vagrant server is 192.168.192.192
On my main computer I run the command ssh vagrant#192.168.192.192 and I login with the default password.

Categories