Building Sample Apps in Ruby, NodeJS and PHP
I know PHP is not the sexiest language in the web world right now, but it has a few things going for it: maturity in the language and frameworks for it, the amazingly friendly community of developers, and how quickly I can get a simple web server up and running.
To illustrate that last point, here’s a quick comparison of what it takes to get sample web app up and running with my three favorite languages / runtimes right now.
Ruby / Sinatra
The fastest way I know to get a Ruby web app up and running is to use Sinatra. I love Sinatra for it’s simplicity and flexibility. I have a handful of projects and websites built in it. But it takes a bit to get up and running. I have to install the Sinatra gem and everything else associated with it, then configure an app.rb file with all the right routes and other configuration options to serve up the project files.
Sinatra is my primary choice when building Ruby apps. It’s great for sharing with the Ruby community because everyone understands it. It’s also cross-platform so anyone on Windows, Linux or Mac can run it. But it is no longer my primary choice for cross-platform demos and sample apps.
I don’t use Rails unless I know I need to build a full stack, server-side MVC app w/ all the logic and complexity that comes along with Rails. This is not a knock against Rails. It’s just “the simplest thing that could work” philosophy, and getting things done.
Node is about the same amount of work to get up and running for a simple demo app. I have to install the Express package through npm and then configure an app.js file to server things correctly. This takes about the same amount of work as Ruby and Sinatra.
There are 3 distinct advantages that I see in Node/Express over, though:
- Jade as default HTML templates
- Easier to run cross-platform
- NPM eats RubyGems lunch
NodeJS is far easier to run cross-platform. Installing and configuring Ruby on a Windows machine is dead-simple. On a Mac? It takes me an hour+ every time I help someone. The community is trying to fix this, but hasn’t yet. I don’t know about Linux, but I’m getting it’s easier on there than on a Mac. NodeJS, on the other hand, is just easier to install and use everywhere. I can share a NodeJS app with any developer on any platform, and be confident that someone who has never installed or used NodeJS will be able to get it up and running in less than 10 minutes.
NPM is also eating RubyGems lunch right now, for simplicity and feature set, because NPM includes what Bundler does for RubyGems. It really comes down to that. I hate having to “gem install bundler” and then “bundle install” separately. That’s stupid. RubyGems should include Bundler in it the way NPM does.
For these three reasons, Node/Express have become my default stack for sharing code with the world. But Node/Express still require more work than PHP, to get a sample web server up and running.
What does it take to get a PHP web server running, so I can begin hacking on a sample app?
php -S localhost:5000
No package manager config files to build. No packages to install. No need to configure a web server file with code. I just type that one liner and I’m good to go. I can edit and display any .php file or .html file that I want, without any other infrastructure work.
The good news is that I can also get something very similar to Ruby/Sinatra or NodeJS/Express with the PHP Slim framework. It’s the PHP counterpart of Sinatra and Express – but it’s optional. I don’t have to use this to get a basic PHP project up and running.
The downside to PHP is that it took me a few days to get it installed and configured on my Mac. OSX comes with a poor version of PHP built in, so I had to install the latest. After many different attempts from many different sources of advice, I finally got it installed through Homebrew. Then I had to figure out why the new version wasn’t the default version. Moving beyond that, I had to figure out that “SQLite” and “SQLite3” are considered separate, unrelated things instead of just versions of SQLite.
Once I got past those hurdles, though, I found myself quite productive with PHP, quickly. The ability to stand up a sample project in one line of terminal commands is quite appealing. I don’t know that PHP will become a regular thing for sharing code, though. I’ll certainly use it when I want to share with the PHP community, or when I need to get a web server up and running in no time flat to server a simple HTML page, though.
I probably need to learn Python for my next web server language. I keep hearing good things about it from a language purity perspective. But that will have to wait until I see another opportunity to learn something new, again.