6 ms·
“Why are you using Node.js rather than PHP?”
- benwerd 13y agoFor 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.
- marxdeveloper 13y agoyou 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 13y agoI'm curious -- what advantages does nginx have over node for serving static files?
- andrewmunsell 13y agoHere's some benchmarks: http://stackoverflow.com/questions/9967887/node-js-itself-or-nginx-frontend-for-serving-static-files 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.
- wmil 13y agoIt 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 13y agoNode 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.
- mcantelon 13y agoFor production node.js apps, it's pretty normal to use both. nginx ends up handling static requests, because that's what it does best, and relaying anything else to node.
- bungle 13y agoNode is better for real-time web in my opinion. That's why, although I look forward nginx + lua in that space.
- bdcravens 13y agoNot really related. In this situation, the author is basically just serving a website based on some dynamic content.
- tmzt 13y agoNot 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 13y agoYeah, if someone digs Lua then using Nginx in conjunction with something like Luvit (https://github.com/luvit/luvit https://github.com/luvit/luvit) might be the way to go.
- tmzt 13y agoOkay, I was wondering about the Lua-in-Nginx case. Using a Lua implementation of something like node.js would certainly work.
- ceol 13y agoI 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.
- HNLogInShit 13y agoTrue, but this was not really well founded: "There is no excuse for displaying file extensions in URLs, it’s an implementation detail that the user shouldn’t care about." The user doesn't care about it. So what does it matter? It's not as if they're typing them in.
- Udo 13y agomysite.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.
- locofacetwice 13y agoif this is his reason to choose node over php he is an idiot.
- efnx 13y agoThe person asking the question is mistaking get/post variables as a php language construct, instead of a web app construct.
- sparkygoblue 13y agoI noticed that as well. I think he was implying that the first URL structure isn't possible in PHP, which of course is not true.
- bdcravens 13y agoLikewise, you can pass and parse URL parameters in an Express app (or any framework for that matter) if so inclined
- adamors 13y agoOr just split the $_SERVER['REQUEST_URI'] by '/' and map the request to pages.
- kawsper 13y agoYes, but that can still be a bit complex. I have used https://github.com/chriso/klein.php https://github.com/chriso/klein.php prior for doing exactly this, but it was a long time ago, the codebase now seems a bit more advanced, and mature.
- bdcravens 13y agoAlot 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 13y ago"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 13y agoOf 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")
- JoshGlazebrook 13y agoI 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.
- jmcdonald-ut 13y agoA bit off topic: I agree that a lot of newbies tend to gravitate towards PHP. When I was 14 I wanted to program, and PHP had such a low barrier to entry that I was able to get coding without any configuration. No working in the shell, just writing a file and uploading it to a shared host. 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.
- ErikAugust 13y agoThat is also DHH's argument as well. 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.
- antidaily 13y ago
- dave1010uk 13y agoIf you want a Node.js style event loop but want to use PHP, give http://reactphp.org/ http://reactphp.org/ a go.
- tmzt 13y agoAlso 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.
- kayoone 13y agoNice answer, but the guy who asked is obviously clueless
- tn13 13y agoI don't see why this post gets so many up-votes. It is so naive.
- antihero 13y agoThing 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.
- spenvo 13y agoOne 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 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 13y agoThis 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.
- spenvo 13y ago*edit --> "lament not having"
- soulclap 13y agoI 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 13y agoCan 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 13y agoSure 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.
- krapp 13y agoHaving worked with both php and javascript I can definitely see how the syntax alone with node.js would be a breath of fresh air to someone used to PHP. Modern frameworks like Laravel do a lot of good to smooth things out but PHP's implementation of anonymous functions, for instance, feels like such a kludge compared to javascript.
- throwaway420 13y agoThis 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 13y agoThis 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 13y agoWhen 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.