Node.js vs Ruby on Rails

Original author: Will Nathan
  • Transfer

Node.js vs Ruby on Rails


Unlike most developers, I started pampering with Node.js, and after that I immersed myself in Ruby on Rails with the amazing Rails for Zombies course and then the Ruby on Rails tutorial by Michael Hartl .
All that is written below is an unscientific comparison of Node.js and Ruby on Rails.

Let's get started!

Node.js - NOT a framework!

First of all, Node.js is a web server with low-level routing and administrative capabilities, written in Javascript with the goal of asynchrony and high speed. And from the very beginning of this comparison, Node.js needs some “friends” in the form of Express.js and Mongoose (unless, of course, you have chosen MongoDB as your database). While Rails, on the other hand, is the most basic framework written in Ruby. Technically, for an honest comparison, we would have to talk here about Node.js vs Thin or Unicorn, not Rails, but we want to develop a complete back-end, and not just its engine compartment.

Ruby is a moody old man with a golden heart.

Now that we have got rid of conventions, let's talk about the language. Node.js is written entirely in Javascript, while Rails is written in Ruby. Both the one and the second have their pros and cons. Javascript plays into the hands of ease of syntax learning, as many probably already faced him in the front-end (against the inherent Rails

:VARIABLE => “хэш-о-хрень”
def func arg = "необязательности скобок у функций"
VARIABLE do |iterator| 
Module::Class декларации классов 
деклараций class Subclass < Class
@instance переменных, взятых из Perl

and other frills).

However, Ruby can be more concise and does not require a forest of brackets (coffescript readers, don't strain) for all of its magic to work. Plus, Ruby (and Rails) is replete with super-useful built-in methods surrounding everything from dates to strings and arrays

10.minutes.ago
“string”.sqeeze
[‘array’,’elements’].include?(‘array’) #true

Yes, all this can be achieved in Javascript, but it will require some knowledge and custom methods ... or something like _undercore .

100% Javascript is awesome, but painfully overrated.

Having understood the languages, you can now pay attention to the development of web applications. Node.js attracts with its asynchronous front-and-back Javascript environment, while in Rails you have to deal with SQL, Ruby and Javascript. Although a seemingly confident advantage - after some study of both, I will write down the uniformity of the language in the category of “ overvalued benefits”" It's not that hard to get into Ruby by reading just a few examples and blog articles. Plus, with Ruby, you can almost completely forget about the “callback noodles” that accompanies Javascript due to its non-blocking architecture (which I absolutely definitely recognize as a plus in the face of great concurrency). And yet, even though I don’t want to say this, sometimes it’s nice to have a predefined syntax for namespaces, classes and modules.

Rails provides everything you need.

Now leave aside the language and its attendant.
Node.js + Express.js provide routing along with controllers, views and helpers. However, this toolbox still fades compared to Rails.
For an analogy, Rails on the background of Node.js + Express.js is like Muji on the background of American Apparel (two large companies selling clothes and home accessories - approx. Translator) . Both provide clothing and home accessories, but American Apparel clothes do not look particularly reliable, do not always meet your needs, try to look cool with their pants, and often do not look with other things. On the other hand, Muji obsessively decorates everything in the world in the same style and, at first glance, with the same fabric.

But back to programming.
Rails comes immediately with a built-in ecosystem, with full MVC and a set of useful features that can only be ruined by critical clubfoot.
Here are a couple of example functions: url_for and image_tag . url_for is a built-in function that will always return the url of the given data object ( @products , for example), and image_tag will always pick up the correct url of the image from the asset pipeline .
Of course, all this can also be done in Node.js, however, this will require considerable work with modules and helpers. Plus, in Rails, you don’t have to worry about data models at all. Yes, in the case of Node.js, Mongoose can help, but even with it it's hard to feel the full control over the data that gives you

Product.find(params[:id])

available everywhere from application, modules, controllers and view in Rails.

However, it is worth noting that all these chips have a downside. I often understand that I simply “guessed” the right path to do this or that, and almost always miss the best path. And also, when something breaks, it can be quite difficult to determine the cause of the breakdown due to tricky special variable names (for example, sticking together the action name + controller name + "_path", you can magically get the new_product_path helper , which returns the current path to the desired action) .
With Node.js, you only see what you wrote yourself - the distortion is minimal.

Many people make and improve modules for Rails.

Next: the ecosystem. Despite the fact that Node.js had a great start with NPM, the dust has not settled yet on most of even the most popular packages for various needs, and the number of competitors is already breaking records. As a result, I often feel like trying to glue a broken vase with superglue.
On the Rails side, it is one of the strongest free repositories of libraries and tools. I also really like The Ruby Toolboxshowing the relative popularity of tools, breaking them down by functionality. Plus, as I have already said, a lot of the modules that you have to type in Node manually, in Rails go out of the box. Things like database connections, profiling, caching, migrations, data models, pipelines, routing, console commands, testing and more ... Even the favicon is already in place! Of course, all this is trivial, but everything is necessary, and it would be cool not to see all these messages about the conflicts of Node.js modules that for some reason refuse to communicate with each other until noon.
Above all this - a huge number of tutorials and examples on Rails, much more than there is on Node.js. One only Railscastswith his 300+ videos. But it is worth keeping in mind that Rails is changing rapidly, and many textbooks have already lost their relevance, and other tasks got their solutions after the publication of various articles with a crawl. As a result, it became wise to use Google selectors < 1 year oldto find answers to questions about Rails, and start this search with Rails Guides .

Rails includes jet-pack. Default.

Finally, suppose you have the same frameworks, databases, the modules are configured and everything is fine. Node.js requires much more time to just write everything and put everything together (again due to inconsistency of third-party plugins and the lack of a rigid structure). In Rails, to build a complete RESTful interface, just one command

rails generate scaffold Post name:string title:string content:text

and the necessary controller, view and model will wait in their places.

Often this takes 1 minute versus 1 hour (multiplied by the number of database entities). This is not such a difficult job, but rather vulnerable to typos, and just a lot of information that you need to remember. I don’t always use Rails scaffolds, but for simple prototypes it’s difficult to come up with a faster alternative.

Finally…

Node.js is a great set of tools, trendy, fast-growing, high-performance (which is debatable), easy to learn and outperforming most other web developer tools. However, he still remains a talented senior star against the Olympic God in the person of Rails. No offense to Node.js, but who would you choose? If you can overcome the inconvenience of learning Ruby, the choice is obvious. Remember: the best tool is one that will allow you to do your job better and faster.

PS From the translator:
Of course, it is more correct to compare Rails not with a pure Node, but with some MVC framework based on it. For example, Sails.js. However, this article carries a certain amount of useful information for a novice web developer (who I was), so I will allow myself a metaphorical comment.
  • Rails is equipped with a huge number of functions and libraries that will make your workplace a reliable and comfortable chopper in the world of web development.
  • Node develops in monstrous steps, providing the developer with a crash-sensitive, but interesting and fashionable sport bike.
Based on personal experience, Node.js + Sails.js gave me an easier alternative to Rails for small projects - and that’s great. But the reliability and strength of RoR in terms of developing a serious web application, they still can not replace, although they catch the soul of the back-end developer.
Thanks to everyone who read it!

Also popular now: