Building Sample Apps in Ruby, NodeJS and PHP

In the last three years I’ve gone from full time .NET development to full time Ruby on Rails and then full time JavaScript. It’s been an awesome ride, so far, but it’s not done yet. I started digging in to PHP recently, and while I’m not going to be doing full time work with it I am certainly adding it to my list languages and runtimes that I enjoy using. 

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.

NodeJS

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

With Sinatra, I have to go through extra steps to install and configure HAML as the template engine. It’s worth the extra 2 or 3 things to do, and I always do it. But with Express, Jade is the default template engine. Jade is HAML++ in JavaScript and I love it more than HAML for it’s even further reduction of noise in HTML tag attributes, etc.

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.

PHP

What does it take to get a PHP web server running, so I can begin hacking on a sample app?

php -S localhost:5000

Done. 

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.

If you haven’t looked at PHP, or it has been a long time (it has been near 13 years since I used it last), I’d recommend checking it out again. If nothing else, it will server as a quick way to get a simple web server off the ground, to start demoing some HTML and JavaScript.

What’s Next?

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.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Community, NodeJS, PHP, Productivity, Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://twitter.com/thefringeninja João P. Bragança

    As someone that started programming with PHP, I’d say that getting up and running quickly is PHP’s biggest flaw. As soon as you want to do more than just a Personal Home Page you are going to be in a world of hurt.

    • jeff_way

      How do you figure?

      • http://twitter.com/thefringeninja João P. Bragança

        Just speaking from personal experience. I spent over a year on a project with the latest PHP (5.4). TBF we had a framework imposed on us for ‘political’ reasons so that may be responsible.

  • http://www.facebook.com/people/Thomas-Brewer/611963861 Thomas Brewer

    PHP has its rough edges, but it doesn’t deserve the hatred it gets from other language communities. It seems a lot of Rubyists are stuck with an image of PHP from the early 2000′s. I personally prefer Ruby to PHP, but at least PHP is better than Python. ;-)

    • jeff_way

      Exactly. Plenty of programmers still cling to this image of PHP as it existed many, many years ago. Need a server? PHP provides one built in. For dependency management, like NPM or Bundler, use Composer. For a modern framework with all of the bells and whistles of a framework like Rails, use Laravel. For TDD, use PHPUnit. Really, the community has matured considerably in the last half-decade.

    • foo

      why would PHP be better than python?

  • http://daniellang.net/ Daniel Lang

    I’m curious why getting up and running a web server seems to be such a big thing for you? When I need to share code quickly, I have plenty of options: Heroku, AppHarbour, Azure Websites and even small things like JsFiddle.

    • http://mutedsolutions.com Derick Bailey

      A large part of my job is putting together samples, creating new samples and showing other people some samples. I spend a lot of time working on sample code, basically. Some of it should not be made public on something like JSFiddle. Plus, JSFiddle (and JSBin and other sites like that) are slow and crippled in feature sets when compared to working with a local server. I can use all my tools with my favorite editors when using a local server vs I have to use a browser with limited tooling on a site when using JSFiddle/JSBin/etc.

      When it comes time to show people the working code, or if I know what I’m doing and I just want to build something to show, then yeah – JSFiddle / JSBin all day long :)

      It’s all about the right tools for the job, as everyone claims. I try to actually put that in to play by learning new tools and having them available for multiple situations. :)

  • http://www.facebook.com/pppdns Dénes Papp

    Thanks for the article!
    Instead of PHP Slim you should take a look at Symfony2 framework. In my opinion it is the most robust, flexible and feature rich MVC framework for PHP. It takes a lot of time to learn it, but if you know it, you can set up a restful api with the minimal CRUD controllers generated automatically for your DB schema in an hour or less

  • Guest

    Im baffled about how it takes you so long to get ruby going on your mac? Took me all of 5 minutes to get homebrew and rvm working??

  • jarrettmeyer

    The mac story is completely dependent on a working Homebrew install. If `brew doctor` returns no errors, `brew install rbenv`, `brew install ruby-build`, `rbenv install 1.9.3-pXXX`, and you’re ready to go. Without Homebrew, yeah, it’s a PITA.

  • Greg

    Starting a webserver with rails is “rails s”. Why is that worst than the new php way? I am working with php for 6 years and with ruby for 3. My experience is php as a language is crap, the community is worst than the ruby/rails one.

    • http://mutedsolutions.com Derick Bailey

      starting rails server? yeah. one line. install, configure, and getting it to the pont where you can type “rails s” to build the most simple static html page is a whole lot more than that

      • rssvihla

        Maybe it’s changed since my sysadmin days (I doubt it as the language has hardly changed in the last 7 years) but the primary reason I went running and screaming from PHP after nearly a decade of configuring and administering it was the nightmare of the runtime.

        Many of you will now tell me “but it’s already installed with XYZ” and I’ll start asking questions about this extension or that extension that you want to use and before you know it, you’ve got a mutually exclusive world where extension A works on php 5.2.11 and extension B requires php 5.2.12 but extension A requires a features that was changed in 5.2.11, and then you’ll have a major exploit in version 5.2.12!

        Maintaining that world for shared hosting was _terrible_ and vendors make it so unbearable that I usually just rolled my own from scratch, which was another whole giant maintenance nightmare when you have multiple severe security exploits per year. Don’t get me started on the mess that can be the PHP.ini and various extensions.

        Saying PHP is simple to get running is like saying that serial killer has such a nice smile, I bet once you get to know him it’ll get ugly quickly.

    • jeff_way

      “The community is worse than the Ruby/Rails one.” Huh?? What does that mean?

      The language, itself, has some oddities; however, a well-built framework removes 99% of those issues. Yes, argument order is confusing at times. That affects my workflow pretty much 0% of the time.

  • http://twitter.com/goranhalusa Gor

    I think installing RoR has just gotten much simpler with “RailsInstaller”, a project by Engine Yard – http://railsinstaller.org/. I haven’t tried it, but it looks promising.

  • Jon Perty

    Interesting article, but I don’t get this obsession with how fast you can get an app up and running. Any project needs a decent UI, content, UX, maybe thought about seo, and lots more, these all take time, usually much longer than it takes to get a basic environment set up, especially if you are an experienced developer.

    Does it really matter how long it takes to set up a language or server on your first ever use of it, whether one minute or one hour? Yes, if you want to try node, its really easy to get it set up for the first go, but if it took an hour, does that make it bad? If all we are striving for is that, then why dont you just use dreamweaver and stop coding all together? I find having control over servers and learning how to set them up fun.

    I don’t think I have ever had to sent someone code where they did not have the environment set up? if they want to see a site, and they havnt, then look at a staging environment or something? All these speed comparisons are for pure hello world apps.. Is it a race to see who can get hello world done? There is no best language or framework. If you need server side code, whether ruby, node, php, python, .net or java, to build a site properly, with clean, maintainable code, it comes down to the developers experience.

  • crojas

    I found an interesting site to build app in PHP, Ruby, Nodejs and other
    check this out: http://weflavors.com

    You can even choose the framework and ui framework

  • crojas

    I found an interesting site to build app in PHP, Ruby, Nodejs and other
    check this out: http://weflavors.com

    You can even choose the framework and ui framework

  • http://www.facebook.com/AMSoft Rakesh Mehta

    Is there any language int this world that does not have any glitch or vulnerability issue? I don’t believe so. Some languages has fewer issues that other. For someone PHP seems to be promising one, for other it’s a crap. These are all personal tastes. The good thing with PHP is that it’s light weight and not that hard to configure (even manually) and you get lots of frameworks and open source projects to start working on. The documentation on php.net is another +, very well indexed in google and thus quickly searchable. I personally believe that to be a good developer, one should have strong analytical and problem solving skill. Good understand of project flow and end users requirement. Without these understanding none of the languages is useful to you. Basically you should know, what you are going to do and then decide the most beneficial (commercially and technically) way to do that. I never believe that for a good programmer programming language is never a barrier. And yes, you must consider the new releases of PHP http://www.am-horizon.com/blog/2012/04/whats-new-in-php-5-4

  • http://twitter.com/lucmendelowski Lucas Mendelowski

    Or you can just type: python -m SimpleHTTPServer :)

  • http://nicoburns.com/ Nico Burns

    I highly recommend the ‘http-server’ node module

    npm install -g http-server

    Then run with

    http-server

    It will only serve static content, but other than that it’s great. My favourite feature is through it auto-increments the port, so it’s really really easy to start multiple servers at once.

  • http://twitter.com/Dmitri145 Dmitri Zaitsev

    Somehow php -S localhost:5000 doesn’t seem to work for me.
    The options -S does not seem to exist and I am sent to the help page of php.
    Am I missing something?

    I have Mac OS 10.8.3 and standard php:

    PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 28 2012 18:19:13)

    Copyright (c) 1997-2012 The PHP Group

    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

    • http://mutedsolutions.com Derick Bailey

      the -S option and built in web server were introduced with PHP 5.4. You’ll need to install that version (or higher, if one is released since) to get this to work

      • http://twitter.com/Dmitri145 Dmitri Zaitsev

        Thank you, that explains it!

  • Davy

    A bit opinionated. If you pick a platform based on how long it takes you to get a new stub project up and running on your local machine, it’s not a good reason, because the setup is a one time task.

    If you have many small projects with different setups, you can consider using framework agnostic deploy/stub/skeleton software like opscodes chef, or brunch for a lighter version to manage your basic setup. You only have to do it once, and you will have your framework (rails, php, sinatra, python, node, whatever) the way you like it.

    The choice of language is a question of language features, maturity and support. In your case it sounds more like its a question of what you have on your mind the moment you turn on the computer…

    Your arguments that it takes time to get a sinatra setup up and running is utterly nonsense. What you really mean is that it takes a while to set it up the way *you* like it to run (with haml, etc.). Does php support haml in the base installation? No.

    If you want a fast setup webserver, most frameworks today have a commandline one-liner: rails, python, php. I prefer 37signals pow myself, because then you don’t have to use the one-liner even.

    Happy coding,
    Davy

  • cashio

    Look at this node server serving PHP and websockets directly

  • cashio

    hmm – no links?
    Its at github paragi/was