“Why are you using Node.js rather than PHP?”
sparklinlabs.com
sparklinlabs.com
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.
URL arguments with '?' and '&' should specify a secondary "meta" mapping for things like sorting or filtering, that still acts on basically the same resource.
/posts/page/2
/posts?page=2
/posts?limit=25&offset=25
I like the third option because it allows the must functionality to the user if they so desire, but it kind of feels like a violation of the "only sorting and filtering" rule.Of course, there are a lot of sites that use .htaccess to implement routing rules, but I'm not a huge fan of that. All things being equal, I believe just passing the virtual path to an /index.php endpoint is preferable - and it's pretty much what other languages and frameworks do in these cases as well.
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.
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.
To be honest, starting with PHP and just going was a really effective way to get me hooked to programming. Eight years later, and I've worked with C++, Ruby, Java, and C# to solve various problems. After seeing what else is out there, and understanding that different languages solve different problem sets, I don't think I'll ever gravitate back towards using PHP if I can help it. Occasionally it's fun to look at some of my old code and cringe. I'm sure if I could find some of my old questions on various forums they would look a lot like the question that user asked.
I would add also that WordPress and Drupal being PHP based also drives the popularity of the language - almost making it the de facto language of the small business freelancer.
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.
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.
http://stackoverflow.com/questions/9967887/node-js-itself-or...
Nginx is pretty well tuned for serving static files quickly and efficiently, whereas Node.js is not. At scale, you're much better off serving files through Nginx because it'll do it faster.
Plus it's considered more secure, and presumed to have a performance advantage.
but yeah, in short nginx performs better and uses less memory.
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 ]
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.
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.