Node.js for Rails Lovers

June 16th, 2019

You love Ruby on Rails. There’s no easier or fun way to build a web application. But maybe you have several projects with specific Node.js business needs, or you just want to learn a new framework. You’re well-versed in JavaScript, so Node should be an easy transition, right? Let’s find out.

TLDR

Convention over configuration

With Rails, you don’t have to think about how to structure your app. According to The Rails Doctrine, “You’re not a beautiful and unique snowflake”. There’s no need to deliberate over the same decisions for every single project. Conventions significantly accelerate productivity. Instead of focusing on mundane plumbing, you can focus on the extraordinary features of your app.

If you want to stick to this philosophy, read no further. Go install Sails.js, which is essentially Rails for Node, and get building.

Sometimes you need something more lightweight or custom than what you’re given with Rails or Sails. If you want to build your own Node app from scratch, read on to see how to piece together all the basics for a streamlined MVC web app.

Decision paralysis is a real thing

Coming from the Rails world where everything is already decided for you, it seems like there’s an endless supply of possible components, file structures, and naming conventions to use in a Node app. It’s easy to belabor over the number of GitHub stars, developer articles, and StackOverflow comments for each potential library. In Rails, you get ActiveRecord. In Node, you get to choose which ORM, if any, to use. In Rails, your controllers go in the controllers directory. In Node, they go wherever you want. How can someone ever get started in this mess of decisions?

In this article, I’ve made some decisions for you. Of course, this isn’t the only way (or probably even the best way) to build a Node app. But it’s a good start, and each component can be swapped with an alternative. What follows is a basic boilerplate to use for real-world Node apps, in the form of a blog.

Note: commands below assume you’re using MacOS. There’s a link to each library, where command line instructions are available for all platforms.

The engine

What do you need in order to get started with Node? You need Node. So let’s install it. It’s a good idea to use a version manager for Node, just like you use RVM or rbenv for Ruby. Install NVM.

Then you can install the latest version of Node (or whatever version you like).

nvm install node

Dependency management

Just like Rails has gems, Node has JavaScript packages. They’ll all be installed under a node_modules folder in your working directory. Instead of Bundler, Node ships with npm, and we’ll be installing global packages with npm. But for our project packages, we’re going to use Yarn, which is slightly faster and strongly binds package versions. Yarn can be installed with Homebrew.

brew install yarn

Once Yarn is installed, you can initialize your new blog project easily.

mkdir blog
cd blog
yarn init

Follow the prompts, and you should now have a package.json file in your project directory.

The framework

Rails is a framework. Ruby is its language. The most popular Node framework is Express. It’s about as minimalist as you can get, and you can use it however you like. Let’s install Express with Yarn.

yarn add express

Here’s the simplest Express app in the world. Create an index.js file at your project’s root.

You should see “My blog is listening on port 3000” in your console. Browse to http://localhost:3000, and you’ll see “I am Node!” in your browser. Congratulations, you just launched your first Node app!

The model

Well, that’s not much of an app. Let’s give it some substance. Most web applications are backed by some persistent data.

The Database

Just like Rails, Node will let you connect any database you like. Here we’re going to use PostgreSQL, but this could be any database.

Many Node tutorials you find online will use MongoDB, which is a viable document-based database. But we’re building a relational database today, so we’re sticking with SQL.

Install PostgreSQL if you don’t already have it, and create your development database.

The query interface

Rails gives you Active Record Query Interface out of the box. Sure, you could write SQL commands, but who wants to do that? We will use Knex.jsfor our query interface, a popular SQL query builder. Add knex to your project.

yarn add knex

Knex gives you all sorts of control over your database. We’re going to use its command line interface for setup, migrations, and seeds. Install the knex CLI globally with npm, and then initialize knex in our project.

npm install knex -g
knex init

That will create knexfile.js in your project directory, which holds our database configurations. Let’s make it ours.

Run the migration, and you now have an articles table in your database.

knex migrate:latest

ORM

Object-relational mapping helps to work with relations between data sets. Rails give you Active Record. Node lets you choose. We’re going to use Objection.js, which is supported by knex. Add it to your project with Yarn.

yarn add objection

Now you can put all your models in a models folder. Create a base model from which all other models will inherit. This will handle our timestamps.

The view

Views in Node don’t veer too far from what you’re used to in Rails. We can use the same naming convention, and keep all our views in a views directory. To render views, Express requires a template engine. There are tons of template engines available. We’re going to use Pug. Install it with Yarn.

Summary

With little effort, you now have a solid boilerplate with which to start your Node projects. The great thing about Node apps is you can pick and choose at a very granular level which pieces you want to include, and which to leave out. This setup will bring you very close to what you’re familiar with in Rails.

I hope you found this helpful! There’s lots more to do: testing, validation, relational mapping, security, partials… the list goes on. Message me your comments with follow-ups you’d like to see.