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.

Update: 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, and I now regret not having used JS and Node instead, because it would have been very much simpler and the result better.

A JavaScript CMS on the server

The LAMP model was not suitable for modern applications, among other reason, due to the lack of an offline mode, not allowed with PHP. Wordpress is the best example. Even if this CMS has many qualities and many advantages over its competitors, notably the ease of use and customization, it is less and less suitable on modern platforms, because of its LAMP background.
As I suggest in this article, new CMS based on JavaScript and Node.js begin to appear. An example with Ghost. This CMS obtained a consequent fundraiser on KickStarter with the support of several companies, including Microsoft, which has invested ($ 50,000 is reported) to support its development.

And the terms of the presentation of the software agree well with my own conclusions:

At Ghost, we feel that not only does the use of JavaScript provide us access to rapidly growing communities of bright minds, but it also makes the project accessible to other communities that have no interest in the PHP projects, which are now old guard. JavaScript has stood the test of time and Ghost, true to its mission, is able to work closely with the communities that push innovation forward on the web.

Apparently many of the comments on the first part, especially on Hacker News, come from the old guard ;)

It remains to show more precisely how the use of JavaScript allows for a more modern CMS.

Personal Dashboard

Wordpress uses widgets to allow the webmaster to make the interface of the site, and it is also possible with a JavaScript CMS. But Ghost goes further, you can also build your own dashboard by adding and moving widgets...

The dashboard is built as a web or mobile application: it fits in a single page while Wordpress uses principles of desktop applications inherited from legacy with a multitude of tab each dedicated to one of the management task of the site.

Edition unconstrained

The creation of a post makes use of two panels: on the left, the "source code", which is not in HTML but markdown instead. And on the right content as seen by the reader.

This choice is justified by the desire to build a simple CMS, limited to blogging, what was Wordpress at the origin. This reduces formatting problems. It is often difficulty to get what we want with the WordPress editor (TinyMCE) because it has its own rules and removes the tags that are inserted when it does not suit it. This is something that is inherent in all these online editors which do not have the capacity of DreamWeaver of synchronization between the source and the display. The markdown has limitations, but its simplicity provides the synchronization to write a post more comfortably.

Access to prior articles and editing them in Ghost is also facilitated by a double window. A list can be scrolled in a window to select a post and to edit it. It is easy to switch from one to another. Wordpress due to its old design, requires moving from one page to another for the same tasks which makes editing more difficult.

Themes and plugins without templates

Making a theme in JavaScript and HTML seems easier than with PHP, and plugins too. A theme here is an HTML and CSS page, with fields dedicated to each part of content. A plugin is a JavaScript file that is loaded into the page or is added as a module to Node.js.

There is no reason to meet these compatibility issues with both other plugins and with new versions of the software, which fulfill Wordpress forums of complaints. Once the software has a modern design based on WebSocket like Advanced Explorer on this site, modules can communicate through messages and the same controls receiving the same answers, different modules can evolve independently without inconvenience .

Running on mobile

The advantage of JavaScript and Node.js is to allow Ghost to run on all devices as an application because it is faster and lighter than Wordpress. The author has access to the dashboard on a tablet or mobile .
It is obvious that removing just that 1300 KB of the TinyMCE editor and 500 KB of jQuery, it significantly speeds up the loading of the dashboard! In fact, the JavaScript directory of Wordpress 2.9 contains more than 3 megas of code.

To access the dashboard on a mobile, we should at least has the offline mode to store all the necessary code locally. But this is not possible with a software written in PHP .

All shared hosting do not support Node.js. But there are such as Gandi. And there is the cloud.

Comments
May 12, 2013 18:33:01

Scriptol

@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.