Why JavaScript on the server?

Replacing PHP by JavaScript, are there good reasons for this?

The same language on the server and the client

This is not only a matter of simplicity or ease of learning. It is a fact that moving from one language to another facilitates the occurrence of errors, but the advantage of the unique language goes beyond. This allows to transfer at your choice the processing to the client or the server. Exactly the same functions with the same libraries.
We can choose to place the code on the client to alleviate the server load because the customer has unused resources. Instead we may transfer code on the server to protect the source if we own it, or to reduce the loading time by sending only the results to the client.

Evolution of Sedan cars: Tucker, Cadillac, Tesla to illustrate moving from PHP to JavaScript

Performances (comparing Node.js and PHP)

JavaScript and PHP are both dynamic and interpreted languages. But the first has a high performance JIT compiler. I did not need to carry out tests to prove it, others have already done and a study shows that Node.js can be fifty times faster than PHP (Update 06/2014: the site is now closed). This is normal, JIT is much faster.
The difference is certainly reduced with Facebook's HHVM, a JIT virtual machine. But this is not the PHP that we find on all servers.

Unlimited libraries without compilation

The number of libraries that can be included in a project, including from GitHub, is an advantage. They can be written in any language (export instructions are just added) and linked to JS with the require command .
PHP also allows you to link libraries written in C, but we must ask the user to enable the extension in the INI file which is unacceptable for distribution when users are not programmers.

Dynamic web application

If you want to use a content manager for a portal or blog, PHP remains irreplaceable. There are managers in JS, but they are not at the level of Wordpress or Drupal. On the other hand, for an online application, there are frameworks made to HTML. For example Angular, Ember, which offers bi-directional data-binding.
The difference is that the CMS sees HTML as a code to produce, and frameworks as an interface with which to interact.
Thus, while the CMS in PHP is suitable for editorial contents, for an original application with new features a framework provides more freedom.
And CMS in JavaScript will grow over time.

Offline and mobile

I've never seen a Wordpress or Joomla site working in offline mode. In theory, a PHP program can run on the client, such as Java applets (this is not a good reference), but that assumes that a PHP interpreter is present. This limitation does not exist for JS, it is present on all computers in the browser.
The offline mode is particularly welcomed on mobile devices, to avoid time-consuming loading at each session but also to save bandwidth that is limited on these devices.

In conclusion, the LAMP model (Linux, Apache, MySQL, PHP) was developed and popularized in the era of computers and office software and a web of static pages, albeit supplemented by Ajax, but it is only a patch of dynamics on a static system.
It is not necessarily optimal for the current devices, mobile, online applications also running offline.

Updated Mai 11, 2013. Amended paragraph about libraries. Following comments from users, I have to clarify something. PHP had a dl command to include external libraries that has been removed since version 5.3. And also include or require command to includes other PHP sources in the current file.
The require command of Node.js allows to link the programm to external libraries in JS or another language, it does not compare.
I must also add after criticism on Hacker News saying that JavaScript has been used since 2000 on the server, that what I'm talking about is a whole ecosystem with Node (created in 2009), V8, Emscriptem, not a single language.

Updated May 13, 2013: After reading the comments of readers here and on HN, I realize that in fact, the advantage of JS over PHP is not in the list of features, but in experience they provide instead. I made ​​some online applications in PHP (whatever one could thinks), and I now regret not having used JS and Node instead, because it would have been very much simpler and the result better. This was the reason for this article and it will need more details in the future.

Part 2: A CMS in JavaScript on the server. A new software to replace Wordpress.

May 12, 2013 18:33:01


@Mitja Guštin Thanks for the info. I have made some experiments to use PHP with Node.js (see the JavaScript section) and on the desktop with HTML for the interface (Link checker in the scripts section and Thumbail Maker included with Advanced Explorer). When we have already a PHP program, it is possible to use it on various environments with HTML and on the desktop. The point of this article is to choose the right language and platform for a new project, between JS and PHP. I believe JavaScript is best suited, even if we can add new tools to extend PHP. They are new perspectives with JS that I have not added to the list because I have not tested them for now, but I believe JS is more promising for future projects. @Joe I have used PHP for years and used the dl command in the past. But as you said, it is discouraged and there are some drawbacks to use it (and even removed in 5.3). This can not be compared to npm and the require command. However the goal of this article is to help to choose the best platform for our next projects and all contributions and infos are welcomed.