If you are running a Node.js Application inside a Ubuntu VirtualBox
machine and want to test the same application in any browser inside your
Windows/Ubuntu Host OS, then follow this small tutorial. You can even
test the application on any other device in same wifi or lan network as
your Host/Guest Machine.
The main reasons I wanted this kind of hack is
Now go to terminal and find ip of your virtual machine by typing
That’s it. Now open any browser in Host machine. Type url with above above IP and assigned
Here is the screenshot from my mobile device
Please comment for any doubts.
The main reasons I wanted this kind of hack is
I am explaining here the steps to make it work.
- The Guest machine becomes slow and laggy if you start any browser in it to test your applications.
- Sometime you just want to use CLI 1.
- Test the Web Application in mobile or tablet device.
Virtual Box Settins
- Make sure the guest OS for which you are setting up is not running.
- Now Open Virtual Box Manager and Select the machine.
- Go to
Settings -> Network -> Adapter 1(first tab)
. Network Adapter should be enabled here.- In
Attached to:
DropDown selectBridged Adapter
- In
Name:
Dropdown, Select WiFi or LAN Adapter to which other devices will be connected. I have selected WiFi because my mobile, tablet and laptop are connected to it.- That’s it. Now start your Guest OS.
VirtualBox Network Settings |
Changes in Node.js Server
I assume you are already familiar with Node.js. Below is the simple code that I have taken from nodejs.org,var http = require('http');
var PORT = 1337; //Can be any open port
var IP = '127.0.0.1'; //for localhost
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(PORT, IP);
console.log('Server running at http://'+ IP + ':'+ PORT + '/');
The only change we have to do here is assingn 0.0.0.0
to IP
instead of standard 127.0.0.1
var IP = '0.0.0.0';
Put the code in a file named server.js
and run node server.js
in it’s file path. $ node /path/to/file/server.js
Node.js Server running |
Now go to terminal and find ip of your virtual machine by typing
ifconfig
. It will give you some local ip address for eth0
. I am getting 192.168.2.10
.That’s it. Now open any browser in Host machine. Type url with above above IP and assigned
PORT
in `server.js like this http://192.168.2.10:1337
For other devices like mobile, tablet or any machine on same network, use same url http://192.168.2.10:1337
.Here is the screenshot from my mobile device
Node.js server on Android chrome browser |
Please comment for any doubts.
- Commnad line Interface. ↩
This method is very useful, it's work fine for me
ReplyDeleteThank you.
@aho : glad to know that you found it useful.
ReplyDelete