You’re a PHP developer. I get it. So am I. My journey to PHP didn’t take the normal road that most PHP developers travel in their quest for the perfect programming language. I initially started as Java Developer and spent roughly 10 years living in that land. I was one of those die hard Java developers that when PHP would get introduced into a conversation, I would start spouting off things like enterprise, scalability and other nonsense.
I started working on an open source project that needed a social web front-end about 5 years ago and the team needed to choose a programming language for the site. I explored Java and most other languages but settled on PHP for a number of reasons. It was tough to swallow my pride and start coding in PHP but what happened during that project was nothing short of a miracle. I fell in love with the language and began using it for as many projects that I could find while leaving my Java roots in the dust. PHP has served me well over the last 5 years but I was still searching for that holy grail of a programming language that is fast to develop in, has enterprise backing, is performant and scalable while also providing a strong community of developers. I believe that Node.js satisfies all of my requirements while still being a fast growing and evolving language.
The first thing you need to understand is that Node.js is not just for the hipster developers or early adopters. It is in use by some of the most highly trafficked website on the Internet today and is continuing to win over developers hearts and mind. It is truly at a point where you can trust it for even the most complicated of systems.
Node applications are normally implemented when you need to maximize efficiency by utilizing non-blocking I/O and asynchronous events. One gotcha for PHP Developers to know is that Node.js applications run in a single thread. However, backend Node.js code uses multiple threads for operations such as network and file access. Given this, Node is perfect for applications where a near real-time experience is desired.
Getting started with a sample project
For the remainder of this blog post, I am going to show you how to get up to speed with Node.js coming from a PHP background. The sample application that we are going to write is a simple backend service that will provide the location of every Walmart store. I chose Walmart for this example because quite simply, it is the holy grail of all department stores. If Walmart doesn’t have it, you don’t need it.
By the end of this blog post, you will see how quick and easy it is to create a REST based API using Node.js that is powered by the popular MongoDB database. I chose this REST based example because creating a backend API has quickly became a common use case in most modern applications.
I had originally planned to create the same application in both PHP and Node.js to help ease the transition for you but in order to cover all of the different frameworks and ways of creating REST based services in PHP, it would warrant an entire book and simply can’t be covered in a single blog post. I then thought about just using the Laravel framework as it is continuing to grow in popularity. However, I would still only reach a quarter of the PHP developers. Personally, my favorite framework is CodeIgniter but it is quickly losing ground and now only represents less than 8% of the PHP developer population. Sitepoint recently published an article discussing this very thing and provides the following chart depicting the frameworks that show the most promise for 2014.
Given the vast differences in how to configure database connections and create REST services for each framework, I will assume that you know how to do this for your framework in PHP and instead will focus just on the Node.js code.
Creating our Node.js application
For the rest of this post, we are going to create the Walmart locator application using the LoopBack API framework from StrongLoop. As an added bonus, I will walk you through installing Node.js on OSX. So grab your cup of coffee, sit back, relax, and let’s get to work.