Learning Node.js After Rails
By: Jacob Short
Tags: JavaScript, Rails, Backend, Ruby, Nodejs

I started learning Ruby and Rails in Flatiron Coding Bootcamp somewhere around October of 2020; which is approximately two vaccines, one election, and two virus spreading holidays ago in 2020 time. That said when we moved to JavaScript (a language I already knew), I decided to test the waters of this fancy frontend language being reconfigured as a backend in what is called Node.js. I have good news for my fellow students and Rails users, this is going to be easy to learn if you paid attention in Rails school.

Unfortunately the same is not true for me as I almost always look like the left photo.

Before we get too far, why and what is Node.js? Put simply, it’s JavaScript but on the backend. It started in 2009 when a guy named Ryan Dahl decided to rip out Google Chrome’s V8 JS engine so the browsers could no longer monopolize JavaScript fun. Node.js also kept the event loop for asynchronicity so JavaScripters would not feel entirely out of place missing their beloved DOM. This led to Node Package Manager (NPM), which is like a gem manager for you Ruby people reading this, and a whole lot of other cool things. Now all of a sudden you have a gateway backend language for anyone who knows JS!

But wait! Rails is a Ruby framework, isn’t it? Where is Node.js’s framework? It’s called Express JS (there are more but Express is used more than half the time). No one wants to write in any vanilla version of a language. That’s way too much work.

So I can hype this up all I want but why not look at the code and see for yourself that this isn’t going to be that much of a stretch for Rails users.

Node.js Class

Ignore the ‘new mongodb’ line as there will be NoSQL here today. Pun intended. The rest should look very familiar as Ruby’s def initialize is JavaScript’s constructor. They do the same thing: handle arguments, among other things, for new class instances. ‘This’ in JS is ‘self’ in Ruby. Null is nil and so on. Obviously, this isn’t the entirety of it all but it’s a good start. Express doesn’t offer the plethora of macros that Rails offers us but I find that is actually not a bad thing as typing it out helps one understand better.

Express is also set up so that the model-view-controller design pattern is easy to implement. Restful routing is implemented almost exactly the same as well. Look at this route in Node.js:

Node.js routes

The purpose of all this is to show that learning one of the most popular backend languages to bolster your skillset and resume will be a walk in the park… well that’s actually kinda not easy in 2020. More like ordering something on Amazon. It will be that easy. Amazon easy.

Don’t worry, I haven’t forgotten about Active Record. First, let’s address the name. Who named Active Record? Did their imagination get flayed by corporatism such that all they can use are lower case letters, primary colors, and names like ‘Active Record’? Rant over. Node.js has access to an Object Relationship Mapper (ORM) known as Sequelize! God, that name feels good after Active Record… Sequelize is great. It does pretty much the same things that Active Record does without the syntax shortcuts (as far as I am aware).

Sequelize examples from their guide

This should look super familiar to Active Record users. A little ad hoc but it is a step by step example.

The main takeaway that I want my would-be Rails readers to have is that Node.js is super popular and we have the skills to quickly pick up another backend language if need be in our future job hunts.