Sponsored by Gluten Free Waffles and Sliced Bananas

Wednesday, May 28, 2014

How I Got Node.js and MongoDB running on Android

1:44 PM Posted by Tyson Nero , , , 10 comments
This past Memorial Day Weekend, I gave myself a personal challenge (derived from a project at work) of deploying a Node.js application using MongoDB to an Android device. After a quick Google search, I realized that such a task was not a simple one, but rather it was going to take a lot of research, trial and error, and not a lot of sleep.

For starters, neither Node.js or MongoDB have support for Android. Even more frustrating, they both do not have installs for ARM processors. My first step was to figure out if anyone had already installed Node.js on Android, and if so, how they did it. The first article I came across with this claim was by Masashi Katsumata: Node.js on Android. Based on the directions and comments, I believed this strategy would work and devised a plan:
  1. Buy an inexpensive tablet
  2. Root the device
  3. Install Debian
  4. Install Node.js
  5. Install MongoDB
  6. Deploy and run the application
Buy an Inexpensive Tablet

My next step was finding a cheap tablet. My choices at hand were Walmart and Best Buy. After visiting them both, I picked up a $70 Visual Land Prestige Pro 7D from Best Buy. Now, I was ready to rock, but truly didn't realize what I was in store for.
http://www.tabletroms.com/forums/pro-7d/6932-%5Btut%5D-how-root-vl-prestige-pro-7d-easiest-method.html

Root the Device (Visual Land Prestige Pro 7D)

With such an obscure device, there was not a lot of support for figuring out how to root it. I first came across a video claiming the Visual Land tablets come rooted but not advertised as such. I downloaded Root Checker, and quickly realized that was not true. Luckily, TabletRoms.com had a couple forum threads dedicated to rooting my specific device. The first tutorial was using Vroot, however, I would not recommend this method as it's reported to install adware, some Chinese apps, and modify the Super User. The other option was to follow a tutorial using Cydia Impactor. After following the instructions, I ran Root Checker again, and this time, the device was successfully rooted. I also installed several helpful tools listed below which made this whole process much simpler.

Instructions:
  1. Download Cydia Impactor 
  2. Follow rooting instructions here: http://www.tabletroms.com/forums/pro-7d/6932-%5Btut%5D-how-root-vl-prestige-pro-7d-easiest-method.html
Other Recommended Installs:
Install Debian

To install Debian Kit, I first downloaded the Debian Kit app on Google play. This doesn't actually install Debian Kit, but runs pre-checks and supplies the needed files and instructions. From there, I followed a couple of tutorials from the author and successfully installed Debian. To my demise though, I began running out of disk space as I progressed, and needed to re-install using a partitioned SD card instead.

Instructions:
  1. Download Debian Kit app on Google play
  2. Format and partition and SD card giving yourself ample space for the Debian install (recommended)
    1. Here's a good tutorial: How to Partition your Android MicroSD Card
  3. Install Debian Kit (read all)
    1. Basic instructions: http://sven-ola.dyndns.org/repo/debian-kit-en.html
    2. Instructions (includes SD card): http://www.doviak.net/debian-kit/HowTo-SvenOla-DebianKit.pdf
    3. See step 2 here: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html
Install Node.js

Firstly, there is Android build support starting in Node v0.11.3 (beta):
https://github.com/joyent/node/commit/5e4e8ec429381a8d1eebe31647e9daaaae42c54b

Regardless, versions before 0.11.3 do  support building Node for ARM architecture.

I came across a tutorial using Node Version Manager to install the beta version with Android support: https://gist.github.com/jtenner/79bb5350c318f7623ca7. However, it failed to work on my device most likely because nvm didn't compile for ARM.

Following this tutorial: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html, I opted to build and install an earlier version of Node. Since I was dealing with an ARM processor, the install had to be configured with special arguments. I also ran into a strange error to do with "blx" requiring minor modification of Node source code. Luckily, the tutorial addressed both problems, and Node.js was finally built and installed.

Instructions:
  1. Follow step 3 here: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html
    1. Note the steps for ARM architecture
      #> ./configure --without-snapshot --dest-cpu=arm --dest-os=linux
  2. If you get this error "For thumb inter-working we require an architecture which supports blx",  you must customize the macro-assembler-arm.cc file.
    Try either of these options:
    1. http://www.yoovant.com/install-node-0-10-on-arm-based-board/
    2. http://www.yoovant.com/how-install-node-js-0-8-arm-based-sbc-fox-board-g20/

Install MongoDB

This is where the nightmare officially began. Not only can MongoDB not be installed on ARM architecture but it cannot be built for it either. There's an existing issue created in Mongo's official tracker, however it's currently unassigned: https://jira.mongodb.org/browse/SERVER-1811. Reading the comments, however, revealed some clues including several forks of Mongo that supposedly build and install for ARM:
After building and installing the first three above, waiting 3-4 hrs for each, and them failing somewhere in the process, I was ready to give up. Then, I came across one final article: Building MongoDB on Raspberry Pi. With mongo-nonx86, I was able to compile and install flawlessly making my application stack complete.

Instructions:
  1. Read the comments on Mongo's open issue for ARM support
    https://jira.mongodb.org/browse/SERVER-1811
  2. Choose a fork to build and install (follow respective instructions):
    1. https://github.com/RickP/mongopi
    2. https://github.com/wtfuzz/mongo-arm
    3. https://github.com/skrabban/mongo-nonx86 (worked for me)
  3. Or, use these pre-compiled binaries: https://github.com/Barryrowe/mongo-arm

Deploy and Run the Application

Getting the application to the device is rather easy:

  1. Copy the application files to a free partition on the SD Card 
  2. USB transfer
  3. Git clone (if publicly available)

It is recommended to deploy files that were freshly pulled from source control before npm install has been run. Native dependencies are built for the platform during install time. If you copy a native module built on Windows to Linux for instance, you will have issues during run-time.

Once the files are on your device, execute npm install and make sure everything completes.

Finally, fire up your node server making sure any database connections are successful.