Monday, July 8, 2013

Node.js = Easier PhoneGap Development

Developing mobile apps in PhoneGap can be difficult and tedious if you have to launch the simulator to test every little change to the HTML. If you're developing a PhoneGap app that doesn't require any native resources, such as a SQLite Database, you can just run your app in a browser and have access to all the development tools available to standard web developers. However, if you need to run a SQLite Database, access a service that violates cross-origin policy, or need to mock some native functionality, running in a browser can be very difficult - enter Node.js!

Node.js is a nice companion to PhoneGap development. Not only because code written for Node.js is in JavaScript (meaning fewer mental context switches), but it is very lightweight, and has extensive community support that has added a plugin or framework for just about everything. In my case, I needed to have a SQLite database for loading MBTiles files, which are just SQLite databases in disguise. I used the Express.js framework to provide a very simple web framework and the sqlite3 plugin, and was able to provide an equivalent database abstraction in less than an hour. Having Node.js running also provides a server to serve up the MBTiles files when I'm running the app in the emulator or on my device as well.

All of the code for this example is available through GitHub: offline_map_demo_using_node.

This project was an evolution from my first demo app: offline_map_demo. I've kept them separate for clarity, and the Node version has many code changes to allow automatic detection of the running state and to add proper abstractions around the bits that get data from the database.

1 comment:

  1. PhoneGap definitely wins the game because of its innumerable benefits. Though there are other tools as well but when it comes ease of use I choose no other tool. I cannot be so judgmental about the other tools, but personally I've worked on this tool and I have got quite satisfactory results.

    ReplyDelete