Monday, 24 February 2014

Accessing Guest (VirtualBox) Node.js Application from Windows/Ubuntu Host

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
  • 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.
I am explaining here the steps to make it work.

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 select Bridged 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
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
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 chrome mobile
Node.js server on Android chrome browser

Please comment for any doubts.

  1. Commnad line Interface.

2 comments:

  1. This method is very useful, it's work fine for me
    Thank you.

    ReplyDelete
  2. @aho : glad to know that you found it useful.

    ReplyDelete