Access server running on ubuntu host from guest in virtualbox - node.js

I have a nodejs project running in ubuntu. so I access it this way:
http://localhost:9000/login
now I am trying to access this server from a guest windows7 which I am running on virtual box.
My ubuntu IP is 192.168.1.13 and my VM network configuration is Bridged Adapter so I am trying
http://192.168.1.13:9000/login
But it does not work
However, when I run in the cmd 'ping 192.168.1.13' It replies successfully.
Could someone tell me what else I have to do to access my server from the guest?

For what you want to achieve, your Node.js application should listen on the specified IP address and port which is accessible from other systems.
You just can't expect the user from outside world(OS) to access your web-application which is running in your system's localhost.
Change your code to something like this for allowing it to listen on APP_PRIVATE_IP_ADDRESS(substitute your IP, which is 192.168.1.13 in this case) :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
// your code
}).listen(8080, 'APP_PRIVATE_IP_ADDRESS');
console.log('Server running at http://APP_PRIVATE_IP_ADDRESS:8080/');
Also, for further accessibility, check How To Set Up a Node.js Application for Production on Ubuntu 14.04.

Look at file /etc/hosts and check how localhost is mapped. Surely it is set to 127.0.0.1 . If you make your nodejs application listen on IP:port 192.168.1.13:9000 you will be able to connect. Or change the mapping of localhost which I don't recommend.

Related

Unable to access localhost:3000 from other machine

I'm having a node.js application running on localhost:3000 and able to access it through my browser. I'm having the following configuration in my server.js file.
here port is 3000
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port);
});
When I try to access my server using my laptop's IP by checking it through what's my ip on google e.g. ip is http://58.27.123.12:3000, it doesn't take me to the requested page. I'm running the application on Ubuntu 19.10.
I'm also able to ping my ip through other systems and it responds successfully. What I might be doing wrong? Thanks
if you want people to access on LAN give them your localIP with port number or if you want to provide access via internet check below links:-
https://localhost.run/
https://localtunnel.github.io/www/
ssh -R 80:localhost:3000 ssh.localhost.run
using this command solved the issue after installing localtunnel

How to host a node server [duplicate]

I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000 and it won't work.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
If you are using a router then:
Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure yourport is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
Go to windows button
Search "Firewall"
Choose "Allow programs to communicate through Firewall"
Click Change Setup
Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (127.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig or ipconfig in the terminal. Then try one of the IP addresses on the left that does not end in .255 or .0
Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201 worked fine in localhost. But http://{ipaddress}:4201 not working in other machines in local network.
For this I updated the ng serve like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig in cmd if your Windows user else you can use ifconfig.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Open "Windows Defender Firewall with Advanced Security"
Select "Inbound Rules" in the left pane.
In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/
Thanks.
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
This worked for me and I think this is the most basic solution which involves the least setup possible:
With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
put this codes in your server.js :
app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
after that if you can't access app on network disable firewall like this :
ngrok allows you to expose a port on the internet with custom forwarding refs:
$ npx ngrok http 8000
First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.
ipconfig
Follow this code...
const express = require('express');
const app = express();
const port = process.env.port || 8000
app.get('/', (req, res) => {
res.send("Hello Network!")
});
app.listen(port, '192.168.77.112', ()=>{
console.log(`Listening port on ${port}`)
});
In Ubuntu you can fix this by allowing a specific port or port range:
sudo ufw allow PORT-NUMBER/tcp
example:
sudo ufw allow 3000/tcp
or a range:
sudo ufw allow 3000:3001/tcp
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
I had this problem. The solution was to allow node.js through the server's firewall.

My NodeJS server is not working in remove server

When I run my NodeJS server in remote server with script "next" for example, it compiles successful and get "Running in localhost:3000" but when I try to enter to ip:3000 didn't get response from server.
What I should do?
Ubuntu server.
The server respond correctly without :3000
Have you tried to open the ports on your server for port 3000? :)
I'm on Ubuntu so this may be different on your system, but i think this should work for you.
ufw allow 3000
If you want to access remotely you cannot listen your server using localhost
Try to listen server in 0.0.0.0
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "0.0.0.0");
Debugging this might follow a path on these lines:
Verify the server is actually running on that machine. Try to connect to it locally from another shell on that that remote/server machine (use curl if the machine is headless). If it's not running locally, you clearly won't be able to connect remotely.
Assuming the service is actually running, determine if you have the right IP address for the remote machine. Running something like ifconfig (ipconfig on windows I think?) will tell you this.
Determine if you can reach the machine at all over the network. You might try to send it a ping, or connect to another service you know it's offering. If you can't reach it, is it running a firewall that's preventing you or is there address translation going on somewhere?

Nodejs server only listens on localhost

I created two virtual machines - One is Windows 7 and the other is RedHat 6.4. I am trying to run the simplest server on the RedHat machine and then access it from the Windows 7 machine. The problem is that I can't seem to make it work!
This is my code (Simplest there is) :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
Tried to listen(8080,"0.0.0.0" and "127.0.0.1"). Both didn't work.
When I try to access with from the RedHat machine with "wget" command it works, but when I try to go to the Windows 7 machine and enter from chrome or even with "telnet IP 8080" it doesn't work.
Both machines have access to the internet and I can also ping from each to other meaning there is connection between the two.
Also I added the port to the iptables in RedHat and I think I allowed it in the firewall aswell. Just added a new rule that allows all ports.
I wanted to add that I also made a Windows Server 2008 machine and I tried to run the node server there and connect to it from my Windows 7 machine, But still the same problem exactly! Can connect from the Windows Server machine but not from any other machine.

Running a node.js server on my VPS on port 3000 and the connection times out

In hostgator I have a VPS running centOS. I installed NodeJS and screen.
I added the following code to a file named index.js:
//1
var http = require('http');
//2
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 3000.');
On 'screen:1' I run the following command:
node index.js
It gives me the console output stating 'Server running on port 3000.'
I switch to 'screen:0' and run the following command:
curl localhost:3000
and I get the following response:
<html><body><h1>Hello World</h1></body></html>
Yet, when I try my server's IP address (substitute the xxx for a real IP address, cause I'm not disclosing my VPS IP address):
xxx.xxx.xxx.xxx:3000
The page never comes up and eventually it times out.
I've tried various ports (8080, 7000) and to not avail.
Do I need to place the iOS project in a different directory.
Currently I have it in /root/Projects/NodeTutorial2/index.js.
What do I need to do to get a hello world response from my VPS?
If you're getting a response from on the box, but not from other boxes, it's almost certainly a firewall issue. Turning off IPTables or allowing the traffic in on the port in question is one option but an easier / more appropriate option is to simply have your app use port 80 (for HTTP) or 443 (for HTTPS). You can either do that by listening to that port on the app directly, or by having a web server that acts as a reverse-proxy for you (e.g. NGINX or Apache).

Resources