
“Why are you using Node.js rather than PHP?” - superchink
http://sparklinlabs.com/2013/05/why-are-you-using-node-js-rather-than-php/
======
Udo

        mysite.com/games/test_game
    
      instead of something like
    
        mysite.com/games?id=test_game
      
      Which would be the PHP solution?
    

I fail to see what this has to do with using PHP or not. The entire question
has a background untouched by any knowledge about either Node or PHP. The
answer, however well and reasonably written, is about Node only. The title
implies a discussion about why to prefer Node over PHP which is just not
there.

Contrary to the title, the subject here is not PHP at all.

~~~
ErikAugust
Heh, agreed - and to think that the "PHP solution" is the latter automatically
means the author doesn't know much about writing web apps in PHP.

One line of mod_rewrite in an .htaccess file to rout everything to a 3 or 4
line controller script is all it takes.

That being said - Node looks cool and I want to build something in it.

~~~
3minus1
rta, he specifically mentions mod_rewrite

------
bdcravens
Alot of the discussions of the language variety seem to be a bit impedance
mismatched: comparing a framework (such as Rails or Express) to a language
(Go, PHP, ColdFusion). I've found that a solid framework is generally
comparable or at least completely changes the conversation (for example: Rails
vs. ColdFusion is pretty lopsided, but compare Rails with a CF MVC framework
like ColdBox is a completely different discussion). Is this due to the fact
that some languages tend to be less likely developed using frameworks (my
experience in PHP) or is that developers using "younger" languages tend to
take a "framework-first" approach?

~~~
habitue
"younger" of course meaning "got popular for web development more recently" as
javascript and ruby are the same age as php, and python precedes it by several
years.

~~~
bdcravens
Of course - I'm thinking in terms of usage, not necessarily the true age of
the language. (As a long-time ColdFusion user, I've had interesting
conversations where someone told me they were abandoning the "old" language,
which came out in 1995, for something newer and fresher like Ruby, which is
only 5 months "younger")

------
ceol
I think that was a great answer to their question. It was succinct, provided
useful information on top, and didn't use it as an excuse to engage in
language wars. Elisée specified his preference without declaring it the One
True Way.

------
JoshGlazebrook
I think a lot of this (this being people like the person asking the question
(probably)) comes down to the low entry bar for creating things with PHP. It's
popular because it's everywhere (I can't think of a shared hosting provider
without it), and because it's popular I think it's where most newbies tend to
gravitate to, I mean really who doesn't want to be with the "popular kids"?

And that's not to say there are not some awesome PHP developers and PHP based
projects out there, because there certainly are. But I think the fact that you
can easily slop together some PHP code into a single PHP file and slap it on a
shared web host and see your work in action very easily leads to the large
number of new web developers using it. Which in turn continues the endless
churn of more and more people choosing to try to pick up the language over
something else that may be more suitable for what they're trying to
accomplish.

In the end I feel it just comes down to ease of deployment, low entry bar to
getting something to work (even if it may not be the best way), and the large
source of example code out there.

~~~
antidaily
"popular kids" use rails. maybe "most popular _among_ kids".

~~~
JoshGlazebrook
PHP is Abercrombie, and Ruby is JCrew. Be popular!

~~~
scarmig
And Haskell is... the hair shirt?

------
benwerd
For what it's worth, ToroPHP allows you to do pretty much exactly what he's
describing in PHP. But as always, you should use what makes you productive,
whether that's Node.js, PHP, Python, Ruby, Perl, COBOL or QuickBASIC. Maybe
not the last one. But then again, maybe that too.

------
dave1010uk
If you want a Node.js style event loop but want to use PHP, give
<http://reactphp.org/> a go.

~~~
tmzt
Also supports ZeroMQ, a websocket protocol extension (WAMP) and async Redis
support.

It's missing SSL server support, an async HTTP client, an express like router
(but that could be built on top of what it does provide).

I recently wrote a program that parses CSV file uploads and sends feedback to
the browser through websocket using it's combined main loop.

------
spenvo
One of "coolest" things about Javascript (that persuades many to try Node) is
that it has a non-blocking control-flow. But if you are coming to Node from a
PHP/Ruby background, you might lament the default synchronicity of PHP. For
example, you query your database in PHP, you can rest assured that your
program doesn't keep running without the DB result. In Node, you either 1.)
descend into "callback hell" or 2.) use some sort of Async module [
<https://github.com/caolan/async> ]. I personally love Node but would say
'Expect to learn new patterns' to anyone making a transition to it.

~~~
gridmaths
This is probably the most common hiccup in getting started with node.

I think this is the reason node.js is either loved or hated : if you get over
this activation energy hump, you love it, if not you hate node.. to a first
order approximation.

Basically, to get over this hump, you need a small piece of code which can
handle a sequence of tasks in order : I wrote serialq npm to a minimal
implementation of this, and there are much better ones for production use.

So, once you have a sequential task Queue, you can use it to program in a more
procedural way to get started with your early apps.. then you will use it only
for a few edge cases like startup initialization, and you will be comfortable
with callback-heaven :]

Callback-heaven is not a new idea.. you may not have heard of CICS COBOL
system.. its an ancient mainframe terminal client-server app framework system
with some of the same ideas.

As a side note, be aware you don't need to have many levels of indentation in
your code.. its a bad symptom in node javascript or in any other language.
Just as novice programmers can make deeply indented code in Pascal or C.. you
can do it in your node.js program. Just realize its bad style and think of
nicer ways to structure it. Async does not imply deep indenting.

------
marxdeveloper
you shouldn't use node.js to serve static files its better to use nginx for
that or if you have more resources available serve your files through a
content delivery network

~~~
johnhenry
I'm curious -- what advantages does nginx have over node for serving static
files?

~~~
wmil
It can gzip static files automatically, resume HTTP downloads, and do other
HTTP tricks that are only safe with static files.

Plus it's considered more secure, and presumed to have a performance
advantage.

~~~
ricardobeat
Node can do all those tricks and more, since it's a platform for network
applications. transload.it built their file upload/transcoding service on
node.js.

but yeah, in short nginx performs better and uses less memory.

------
tn13
I don't see why this post gets so many up-votes. It is so naive.

------
throwaway420
This article is a bit weird to me. Node.js is a fine choice for a real-time
web game or something as he mentioned. But it's not as good of a choice as PHP
for simple, mostly static, small business websites that maybe need just some
simple DB access and a form or two. The mere fact that PHP hosting is nearly
ubiquitous makes it a wiser choice for many types of simple projects.

~~~
gridmaths
This is debunked, in my personal experience. If I want to do internet data
piping, data conversion or a database backend, its faster and simpler to do it
in node.

I also use node as replacement for scripting in bash, PHP and perl. I also use
javascript for frontend work, so this means I need to keep only one
programming language syntax in my muscle memory.

I've actually used node for quite a few commercial data back-ends : both nosql
( mongo, redis ) and relational ( postgres, mysql ). Ive written similar
things in PHP, so I can compare them fairly : basically there is just more
ugly syntax with PHP.

I use a node proxy to handle multiple small sites on one host, and have a
small node program for hosting static content [which does gzipping, sets
cacheing for images to help page load performance]. It might be slightly
better to use nginx.. but for a small to medium traffic site, this is fine - I
don't want to know the nginx or apache incantations to do this, its just
faster to write code in node.

The main npm modules are pretty stable and cover nearly everything. The
language is _better_ [ smaller simpler more readable code, less syntax, less
ugly syntax, functional idioms, procedural idioms, readable map/reduce,
readable 'functions as variables', better names, less UPPERCASE ugliness, less
work ]

Yes, node is newer, yes there is a mindset change needed to handle the async-
callback-centric approach. But basically node is a better PERL than PERL, a
better bash than bash, and a better backend web language than PHP.

You'll write smaller nicer code.. its probably just as fast or faster these
days at runtime [ although that is not my main reason for choosing it : my
reason for choosing node is less PAIN.. and more enjoyment ]

~~~
throwaway420
When you're dealing with a small amount of code, ease of hosting matters a lot
more than programming language elegance. PHP hosting is available everywhere -
that's what makes it a great fit for many small business projects.

The relative merits of PHP are not in dispute. PHP is less elegant than just
about every programming language out there. It's not necessary to write a
marketing brochure to expound on Node.js's sublime beauty in comparison to PHP
because everybody knows that's the case.

------
antihero
Thing I found about PHP is, whilst it's possible to write decent thing in it,
and you can try all these solutions, why not just switch to a language that is
consistent and powerful like Python? I mean if you're at the level that you
are thinking these things, it's not like you are someone using $1/mo webhosts
that don't offer some form of WSGI support.

------
soulclap
I am far from an elitist (mainly coding PHP for a living prevents that anyway)
but I never thought I'd see posts like this on HN with a decent amount of
upvotes. It seems that anything involving PHP bashing gets upvoted instantly
lately.

~~~
gridmaths
Can we compare PHP and node.js without it appearing as bashing ?

Im critical of PHP, my impression is it has no advantages apart from being
more established. I've written code in both, to do similar tasks so feel I can
compare them fairly.

My impression is PHP is more messy, with no real advantages to balance out
what I would call 'legacy syntax cruft'. I really see node.js as a natural
evolution from PHP.

To be fair, its well known that Javascript has some bad parts, some of its own
legacy syntax crud, but you can write in a modern style that avoids most of
that - so I think its an order of magnitude less cruft in practice than PHP,
and small code size reflects this assertion.

I know modern PHP in OO style can be more readable, more amenable to
programming in the large than old-skool top-to-bottom PHP... but I think the
lispy functional features of Javascript lead to more readable 'code as
thought' : map/reduce, passing functions in variables in practice is more
useful to me than OO is, it leads to simpler code.

A lot of people feel emotional attachment to PHP, if you love it use it. But
all the things I do love about PHP, I love more about the node.js/javascript
combination. You might like it too.

~~~
soulclap
Sure we can. And Node.js definitely shows off some functionality that PHP
should be jealous of (referring to that 'PHP is meant to die' article). I am
probably going to experience it myself soon when tapping into Node.js
territoriers. But it's not a proper comparison if you put plain old basic PHP
up against an ExpressJS project instead of using a current PHP framework such
as Laravel 4.

------
bungle
Node is better for real-time web in my opinion. That's why, although I look
forward nginx + lua in that space.

~~~
tmzt
Not sure where nginx and lua fit into real time, as in main loop and callbacks
from network sockets, Nginx's Lua support seems to be more request-oriented,
executing a block of code in response to a request.

~~~
mcantelon
Yeah, if someone digs Lua then using Nginx in conjunction with something like
Luvit (<https://github.com/luvit/luvit>) might be the way to go.

~~~
tmzt
Okay, I was wondering about the Lua-in-Nginx case. Using a Lua implementation
of something like node.js would certainly work.

------
kayoone
Nice answer, but the guy who asked is obviously clueless

