Erick Ruiz de Chavez

Web Developer, Geek "to the bone", Happy husband.

Developing Node.js Applications on Windows

Node.js is becoming a really popular development platform. It is originally designed to work on *nix environments and most of the modules are now working properly on Windows thanks Microsoft support, but most of the modules does not necessarily means that the modules you require will actually work; one of them: Mongoose, a driver to work with MongoDBUpdate: I just found that Mongoose is already working on Windows, but this guide is still valid if you need to work with a Linux server. To solve this situation I had struggled through different configurations and tests and so far the nicest one for me is the one I’ll describe below.

Considerations

  • It is not required that you know how to use a Linux distribution, but it is highly desirable since you’ll be doing several things in there. If you have questions about Ubuntu, I might be able to help you a bit but I am not a Linux/Ubuntu expert; I highly recommend to use Ubuntu’s forums and of course, Google.
  • As this guide is focused on developers that want/need to work with Node.js I will assume you already know what Node (and development) is.
  • You do not need to be a Virtual Machine expert, but again, some previous knowledge is recommended. Again, I might be able to help you a bit as well as Google.

Let’s rock.

Get VirtualBox

We’ll be doing our development on VirtualBox, so let’s grab the installer and install it on our machine. It is a really straight forward setup: Next, Next, Next, … Finish!

VirtualBox will add a new virtual network device to your machine, this requires administrator privileges and also will make your PC to lost connection for a couple of minutes. If you are downloading something else you may want to finish it before.

On my laptop this virtual device makes my WiFi (using Odyssey Access Client) to think I am always connected, you may want to disable it (as I did) after the installation is done.

[gallery numberposts=“11”]

Get Ubuntu ISO, preferably Server version

Download an ISO image of the easiest Linux distribution out there: Ubuntu. Remember, we are setting a development environment to program some awesome node.js code and do not want to waste several hours/days setting up a custom kernel and all that. I recommend you to get the Ubuntu 11.10 Server ISO because:

  1. It is the one used on this guide
  2. We do not need the complete UI, software, etc. that will make our virtual machine heavier to start and run, we just need a basic server to install node and some extra packages to do our job.

Create a Virtual Machine

Create a VM is really easy. We just need to set some specific values and most of them are the default ones provided by the program itself.

[gallery numberposts=“12” offset=“12”]

Install Ubuntu

We are on a VM on a text installer, so there is no mouse support; don’t worry just click inside the VM window and move with the direction keys and Tab key on your keyboard when required.

  • Start your VM.
  • Select your language and start install process.
  • Choose your system language and keyboard layout, configure network and hostname (I like to keep defaults :)
  • Select Yes if prompted to write changes to disk (there is no risk, we are using a virtual machine and the disk is actually just a file).
  • An important point of the installer. If you are always behind a proxy, you MUST set this when prompted to do so. If you are not sure, you can leave this empty, I have more details regarding proxy settings later.
  • I dont like automatic updates on my VM, so I wont select them. If you prefer, you can go for it.
  • I will also select OpenSSH server because we will use it later; if you forgot it or did not select it, you can install once linux is ready by doing: sudo apt-get install openssh-server
  • If at the end of the installation the installer prompts to remove the install disk, just select ok.

[gallery numberposts=“30” offset=“24”]


Get Putty and forget about yout VM Window

Using the VM window is not always the best option, specially if you need to do setups, copy and paste lines, etc. We are going to let the VM to receive connections using some ports. We’ll setup ports 22 to accept connections to our ssh server and make our life easier also we’ll add port 3000 that is defaults express port.

  • On your VM window select Devices menu and then, Network Adapters
  • Click on Port forwarding
  • On the opened window add (by clicking on the green “+” button) 2 rules, one for 3000 and one for 22 (you can add more ports now or later).
  • Click on OK buttons until all dialogs are closed
  • Download PuTTY (if you dont already have it)
  • Execute it and use localhost as your Host Name and Port 22
  • After click on connect it will bring a one time message, click on Yes
  • Login as your ubuntu user

Congratulations!, you are now connected to your Linux VM from your Windows desktop.

[gallery numberposts=“8” offset=“54”]

Add proxy settings if required

If you require to use proxy and you didn’t set it up during installation you’ll need to export some environment variables. This can be done automatically on your .bashrc file but since I constantly change between Proxy and Non-Proxy settings I prefer to have a simple script to setup my proxy settings when I need them. To do so, follow the next commands:

  • Move to your home folder by just doing:
1
2
cd
vi proxy
  • Hit “i” to enter —INSERT— mode
  • Copy next text and paste it on yout putty window by right clicking it
1
2
3
4
5
6
7
8
9
10
#!/bin/bash
# Note: Replace proxy:port with your actual settings

http_proxy=http://proxy:port
https_proxy=http://proxy:port
ftp_proxy=http://proxy:port

export http_proxy
export https_proxy
export ftp_proxy
  • After pasting it, hit ESC on your keyboard and then type:
1
:wq
  • With the above instructions you now have a text file named proxy. We’ll now set execution permissions:
1
chmod +x proxy

The file proxy is now our custom command to set our proxy settings whenever we need them instead of permanent settings. To enable proxy settings on the current session we only need to do:

1
. ./proxy

Note: All dots are required, that is ‘dot’ ‘space’ ‘dot’ ‘slash’ and the word ‘proxy’

Update and install required packages

On your putty window, enter the following commands:

1
2
3
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential dkms linux-headers-`uname -r` libssl-dev

Note: The uname -r part is required, what it does is to dynamically get the kernel release of our current installation and append it to the ‘linux-headers-“ text

Install guest additions

  • On the VM window, select Devices menu, then Install Guest Additions.
  • Now, back to putty, execute:
1
2
3
sudo mount -t iso9660 /dev/cdrom1 /media/cdrom
cd /media/cdrom/
sudo ./VBoxLinuxAdditions.run

Note 1: The device might be different based on your VM setup. It might also be: /dev/cdrom, /dev/cdrw, /dev/dvd. Note 2: The command might end on “Installing the Window System drivers …fail!” this is because we are on a server installation, hence there is no graphical UI. Do not worry, we now have what we need.

1
2
cd
sudo umount /media/cdrom/

[gallery numberposts=“1” offset=“62”]

Setup shared folders (and share something)

One of my favorite options on VirtualBox besides the port forwarding is the option to share folders. This allows me to read and write content from my guest.

  • On the VM window, select Devices menu, then Shared folders.
  • Click on the small folder button with the green “+”.
  • On Folder path enter or search for your desired folder.
  • Select OK on all dialogs after you are done.

[gallery numberposts=“3” offset=“63”]

Now, before we can actually use the shared folder(s) we need to add our user to a special group. Back to putty!

1
sudo usermod -a -G vboxsf `whoami`

Note: since I do not know your user name, I am using again a command to automagically put your user name instead of mine :)

After above step is done, you’ll need to close your current session and start a new one. Just type ‘exit’ or ‘logout’ and putty should shut down. Open it again, login using same data as above and you will be now able to read and write data to your shared folder(s) from linux to windows.

Your shared folders will be under /media and the names you selected will autoprepend “sf_” to the names, so in my example my folder “Erick” inside linux is /media/sf_Erick/

A last optional (but recommended) step is to create a shortcut to your virtual machine in your desktop so you can start it without actually opnening VirtualBox window first. This is easily done by opening the VirtualBox window, right-clicking your desired VM and selecting the option “Create a shortcut on the desktop”.

[gallery numberposts=“1” offset=“65”]

With the above steps we are done with our Linux installation and preparation. From now on everytime we want to use our linux for node development we just need to start our VM and connect to it using putty (or just use the VM window, but I do not like it)

Install node

On our putty shell, execute:

1
2
3
4
5
6
wget http://nodejs.org/dist/v0.6.15/node-v0.6.15.tar.gz
tar -zxvf node-v0.6.15.tar.gz
cd node-v0.6.15/
./configure
make
sudo make install

Install express as a global command

On our putty shell, execute:

1
sudo npm install express -g

Create a project

Now here comes another tricky part… by some unknown reason to me, npm fails to install modules inside any place of my shared folder, but I have an easy way to workaroud this to continue working on windows and just executing my code on Linux :)

First, I need to create an express project on my windows machine, so I execute express and provide it a path to my windows desired folder:

1
express /media/sf_Erick/Desktop/helloworld

[gallery numberposts=“1” offset=“67”]

Since sf_Erick points to my user folder, now I have a folder on my desktop called helloworld. That will be my main development folder where I’ll create all kind of awesome code. You can think “ok, we just need to copy each file from windows to linux when we update it” but there is actually an easier way to do it :)

We’ll replicate our folder using a linux command called rsync and save it as a “custom” command so we do not keep typing all everytime. I’ll use absolute paths so you will need to update those to reflect your current settings:

1
2
cd
vi run
  • Enter edit mode typing “i”
  • Copy and paste the following text and update the initial path to where you created your node projects. In my case I like to keep some of those on my desktop.
1
2
3
#!/bin/bash
rsync -ruv /media/sf_501677641/Desktop/$1/ /home/`whoami`/$1/
node /home/`whoami`/$1/app.js
  • Hit ESC and then
1
:wq
  • Now, set execution mode to run by doing
1
chmod +x run

What we are doing with those commands is sync the express app provided as a command parameter and all its content from /media/sf_YOUR_FOLDER/YOUR_PATH/ to your home folder, and once it is done, execute it.

To execute my run command for “helloworld” I just need to do:

1
./run helloworld

The very first time you execute it it will end with an error; this is because we do not have the node modules on the folder. to fix that cd to your newly created folder and do:

1
npm install

Now, go back to home by doing cd and run it again

1
./run helloworld

And you should get a wonderful “Express server listening on port 3000 in development mode”

To test it, open your browser and navigate to http://localhost:3000

To edit your files, simply open on windows your project and update it. I also like to recommend to download (and buy) Sublime Text (2) that is an AWESOME code editor. I usually create a project inside my windows folder so I can double click it and start working on it. After any edit I want to test:

  • I save my files
  • Switch to putty
  • Ctrl + C (to stop current express server)
  • ./run MY_PROJECT
  • Back to browser and refresh

[gallery numberposts=“1” offset=“68”]

Comments