Rails to Node.js File Structure That Makes Sense

Tobie Tsuzuki
3 min readJan 13, 2020

--

My programming journey started with Ruby and naturally led into setting up my first backend with Rails. This was a great entry point because rather than worrying about all of the little quirks with setting up a backend, I could just stick with following the Rails convention of setting up everything. I could simply follow any tutorial and learn what to do easy as that.

Then I made the switch to Node to understand how backends work and what you can do with the freedom that Node offers. I enjoyed the process of learning what Node has to offer, but quickly found out that without the convention of Rails, everyone has their own spin on setup. When you’re learning this can get confusing super quickly and as all tutorials and references can be different. So I am writing this blog series to be a reference to all of those coming from Rails to present you with a more familiar file structure and Node setup.

Convention or Freedom

This is the main difference between Rails and Node. If you follow convention, Rails will implement its magic and setup everything you need and all you have to do is touch up some code here and there. It feels like magic, and setting up a Rails backend can be super quick. Yay! The drawback is, you put enter a route wrong it may just not work. Enter Node, a playground in which you can configure anything you want the way you want it. No longer do you need to remember any capitalization or if you need to make a route plural or not. Just do it the way you want.

So Let’s Do This!

Make sure you have Node and Node Package Manager installed then we will start running by this:

npm init -y
npm install express knex pg

These commands setup a package-lock.json file which tracks all of your dependencies, and installs Express, Knex, and PostgreSQL. At a high level:

  • Express: A framework that handles all HTTP request and responses
  • Knex: A SQL query builder for Javascript
  • PostgreSQL: A open source relational database

You will typically see some sort of frameworks that help with communicating with the database and information from the web and these are generally the standard set up so thats what I will stick with.

Lets Talk File Tree

Below if my basic setup that I make:

db/
|
database.js
queries.js
migrations/
|
<numbers>_<migration_name>.js
seeds/
|
01_<model_name>
routes/
|
<model_name>.js
index.js
knexfile.js
package-lock.json
package.json

So at the root level you have 4 files:

  • index.js — Is basically your app. It acts as your server and runs all of your files, queries, and server. Also think of this as the applications controller in Rails.
  • knexfile.js — Your database connection configuration file. Similar to the database.yml file in Rails
  • package.json and package-lock.json — Dependency tracking information. It is the javascript version of a gemfile.

Then we have our folders:

db — Contains interaction with our database.

  • database.js — We export our connection to the database with our database config.
  • queries.js — Is where we will export all of our actual queries to the database, written in Knex. Think of this as your actions in your controller in Rails.

migrations — Contains migrations and schema similar to Rails.

  • <number> _<migration_name>.js — A file that is made for you when you run the knex command:
knex migrate:make

seeds — Contains all of you seed files that run in order similar to Rails.

  • 01_<model_name>.js — It is common to see the number 01 at the beginning to recognize the order that the seeds will run .

routes — Each file here are combinations of routes and controllers in Rails.

  • <model_name>.js — This is where you store what your route endpoints will be and what actions/queries will run when you call upon those end points.

Hope this helped shed some light on what specific files are doing in Node and how helpful structuring your Node project can be. Next time I will add to this series with more specific instruction on how to configure each file.

--

--

Tobie Tsuzuki

A Software Dev, Outdoor Enthusiast, and Coffee Lover in the Denver Area