Can't access a node express app from vagrant host machine - node.js

I am new to networking and assigning ports and things of that nature. I have been using vagrant for some time and have never had any problems trying to get a test environment up and then accessing it through the host machine browser. The set up for this in my Vagrantfile is this:
# network stuff
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "test-box-debian"
Now I am trying to learn a bit about node.js, and every tutorial says I can run npm start and indeed this works fine. I can call wget localhost:3000 (port 3000 being the default in express) and in return get the index.html default page from express.
However when I try and access `192.168.33.10:3000' from the host browser, it doesn't work. I can run netstat and get the following as a result:
sudo netstat -ltpn | grep 3000
tcp6 0 0 :::3000 :::* LISTEN 17238/node
I can see that something doesn't look right but I just don't know enough about ports and networking to know what is wrong and how to fix it.

First, ensure your server is listening to the right IP and that you haven't bound the Express listener elsewhere:
.listen(3000), NOT .listen(3000, '127.0.0.1')
Alternatively, try binding the Express server to your private IP or to the wildcard IP and see if that resolves your connectivity issues:
// Wildcard (All IP's) binding
.listen(3000, '0.0.0.0')
// Specific binding
.listen(3000, '192.168.33.10')
Lastly, port 3000 may not be accessible from the host. If none of the above options in your server code work, try adding the following line to your Vagrantfile:
config.vm.network "forwarded_port", guest: 3000, host: 3000

Make sure you don't have a firewall on your VM blocking the port:
sudo iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
Found the answer over at https://stackoverflow.com/a/28474080/1772120.

If your vagrant setting is like
# network stuff
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "test-box-debian"
Then your node app should listen to 192.168.33.10:8000
const http = require('http');
const hostname = '192.168.33.10';
const port = 8000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type','text/plain');
res.end('Hello World\n');
})
server.listen(port, hostname, () => {
console.log('Server running at');
})

Related

Cant access nodejs server using machine ip address instead of localhost

I am using windows 10 as OS
Iam trying to access my node.js server from another device browser in the same network using my device ip address in our network--> ipv4 192.168.X.X " its obtaind using ipconfigin cmd" and port "4000", i did the following configration in my code:
module.exports = {
server: app,
start: (port) => {
const PORT = port || process.env.PORT || 4000;
server.listen(PORT,'0.0.0.0', () => { console.log(`Listening on port ${PORT}`); });
},
};
I also opened the inbound ports from windows firewall rules for ports 80, 443, 4000.
FireWall Rules Image
When I tried: http://localhost:4000/ -->it works.
When I tried: http://192.168.X.X:4000/ -->it didn't work.
The weird thing is that when I run the same setup on Linux "ubuntu" it works with the same code above "after opening the ports using: ufw allow 80,443,4000 proto tcp".
For debugging with cmd I tried:
netstat -a -o and I got that node is working in localhost:4000 rather than 0.0.0.0:4000
EX: TCP 127.0.0.1:4000 DESKTOP-T18TEC0:0 LISTENING 11628
How I can make it 0.0.0.0:4000 so I can access node.js server by device-network IP 192:168.X.X?
Ping my ip "192.168.X.X" and its pingable.

Nodejs on linux not accessible outside Server

tried everything...
port 8008 seems to be open but no luck.
netstat shows 8008 to be listening
I can do curl localhost:8008 but not from an external machine using the ip address of my server
and yes, i want to host my nodejs on port 8008 (not 8080 - im using 8080 for something else)
netstat output:
tcp 0 0 0.0.0.0:8008 0.0.0.0:* LISTEN
Have you tried changing your port
var port = process.env.PORT || 8008; //server.listen(3000, '0.0.0.0');
app.listen(port, 0.0.0.0, function() {
console.log("Listening on " + port);
});
If you have .env insert value PORT=8008
and run your server

throw err when connecting to mongodb in VM using node.js

I'm running my mongodb in virtualbox using vagrant. I'm trying to connect to the database on my host machine using node.js, but I get thrown a strange err when trying to do so. This is the code i'm running.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:6600/test";
MongoClient.connect(url, function(err, db){
if (err){
throw err;
}
console.log("Database created!");
db.close();
});
This is the error that is shown in command line.
C:\Users\Morgan\Desktop\testingGrounds>node createMongoDB.js
C:\Users\Morgan\Desktop\testingGrounds\node_modules\mongodb\lib\mongo_client.js:421
throw err
^
[object Object]
And this is a relevant part of the Vagrantfile.
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# nginx
config.vm.network "forwarded_port", guest: 80, host: 6600
# # development site
config.vm.network "forwarded_port", guest: 3000, host: 6660
# db browser port
config.vm.network "forwarded_port", guest: 7474, host: 6666
# test port
config.vm.network "forwarded_port", guest: 8800, host: 6606
If you are using vagrant for some dev/test running on local, just use a static ip it will simplify your life.
You can remove all the forwarded ports line from your Vagrantfile and replace with:
config.vm.network :private_network, ip: "192.168.33.10"
In the VM, you need to make sure mongo is bound to this IP or 0.0.0.0 so it can listen to all network interfaces, in the /etc/mongod.conf file, make sure to have
bind_ip=0.0.0.0
or
bind_ip=192.168.33.10
In your node code, you will need to replace the url for mongo to mongodb://192.168.33.10:27017/test (assuming mongo is running on port 27017; if you have made a change in your conf file, align here)

Unable to connect to Node.js in Vagrant

I am trying to set up a Node.js app using Vagrant. There is also a Rails app inside that very same Vagrant box, which works OK. Node is using port 3001, so here are the config settings in Vagrantfile:
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "puppetlabs/ubuntu-14.04-32-puppet"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, host: 4567, guest: 3000
config.vm.network :forwarded_port, host: 5678, guest: 3001
So, I started Node in Vagrant box. This shows that inside the Vagrant box it responds to requests on port 3001:
vagrant#localhost:~$ wget 127.0.0.1:3001
--2014-12-24 06:37:40-- http://127.0.0.1:3001/
Connecting to 127.0.0.1:3001... connected.
HTTP request sent, awaiting response... 200 OK
The guest machine is listening to port 3001:
$ sudo netstat -ltpn | grep 3001
tcp 0 0 127.0.0.1:3001 0.0.0.0:* LISTEN 1422/node
The host is listening to port 5678:
sudo netstat -ltpn | grep 5678
tcp 0 0 0.0.0.0:5678 0.0.0.0:* LISTEN 30966/VBoxHeadless
And yet, I can't connect to the app from the host browser.
What can be the problem?
Updated: Output on vagrant up:
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 3000 => 4567 (adapter 1)
[default] -- 3001 => 5678 (adapter 1)
Got the answer from a colleague.
Turned out, the node server in vagrant was listening to localhost, while it should have been listening to 0.0.0.0:
so this line in www file:
var server = app.listen(app.get('port'), 'localhost', function() {
had to be changed to:
var server = app.listen(app.get('port'), '0.0.0.0', function() {
All's working now.

Mac OS X, Vagrant, Docker, Nginx, Node - how do ports play together?

I have a simple nginx, node.js setup that I want to migrate into the Vagrant, Docker world. I'm not sure how to handle IPs and ports.
My goal is it to see my Hello World in the Browser, best case on port 80, simply by calling my host http://example.com.
UPDATE
I found the solution myself - I created a simple boilerplate here:
https://github.com/ezmilhouse/docker
Feel free to go on from here.
app.js
var app = express();
app.route('*').all(function(req, res) {
res.send('Hello World!');
});
app.listen(2000)
nginx.conf
upstream example.com {
# using the vagrant private network IP (I guess?)
# using the node port
server 192.168.33.10:2000
}
server {
# ports nginx server is listen to
listen 80;
listen 443;
location / {
# upstream proxy
proxy_pass http://example.com;
# ...
}
}
Vagrantfile.proxy
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision "docker"
# settimg a hostname that matches nginx upstream (I guess?)
config.vm.hostname = "example.com"
# setting a private network IP, node.js and nginx run on this IP (i guess?)
config.vm.network "private_network", ip: "192.168.33.10"
end
Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "nginx" do |app|
# forward the nginx port (I guess?)
app.vm.network "forwarded_port", guest: 80, host: 80
app.vm.provider "docker" do |d|
d.build_dir = "./docker/nginx"
d.vagrant_vagrantfile = "./Vagrantfile.proxy"
end
end
config.vm.define "node" do |app|
# forward the node port (I guess?)
app.vm.network "forwarded_port", guest: 2000, host: 2000
app.vm.provider "docker" do |d|
d.build_dir = "./docker/node"
d.vagrant_vagrantfile = "./Vagrantfile.proxy"
end
end
end
/nginx/Dockerfile
# ...
EXPOSE ["80"]
/node/Dockerfile
# ...
EXPOSE ["2000"]
Mac OS X /etc/hosts
192.168.33.10 example.com
I was expecting to call http://192.168.33.10 in the browser to see my Hello World via nginx and http://192.168.33.10:2000 to see the node instance. Therefore the host example.com does not work either.
What do I do wrong?
I created the fork and made pull request to your repo.
The main idea: do not use provisioning for docker containers.
More information your can find in this article

Resources