The Javascript fatigue has been wildly discussed in the internet lately. Some people are more chill and optimist and others not so much. But how can we make this ecosystem easier for new comers ?

I came with a Ruby background searching for a new language and tools to create what I was already creating, which was basically front ends and APIs. In paper everything is pretty straight forward, but it's not that easy. I hope this post save you a lot of time.

The Good Ol' Days

The good ol' days of Ruby. What a time to be alive, 15min of code porn building a blog, so good. If there is something that symbolizes it for me is this:

$ rails new happiness

Bam ! Everything ready, let's code. I always thought of how incredible this moment was compared to my old Java stack for web. To be fair most of the time I was not even using Rails but the simplicity is something truly powerful. When I arrived in the Node/JS world my first thought was:

- Where is my rails new ?

I have to warn you that the things in the Javascript forest are very different, it has a lot of packages, frameworks and it's a lot more opinated too. What I will show here for you ? A list of good packages with very similar features that you probably love in our beloved Rails and some comparison between them. Come with me, but not by train.

Assertion and Mocks

Ruby introduced to me to the real world of testing. Before it I used JUnit on Java and well, this is a sad story. I use Chai and Sinon for testing. Chai has different types of assertion for TDD/BDD and Sinon is pretty simple and standalone, sounds like they were really made to each other. Let's do some comparison of the ruby's Rspec + Mocha:

Rspec and Mocha

describe 'mocking with RSpec' do
  it 'passes when it should' do
    receiver = mock('receiver')
    receiver.expects(:message).once
    receiver.message
  end
end

Chai and Sinon

it('passes when it should', () => {
  const definition = {
    message: function() {}
  }

  const receiver = mock(method)
  receiver.expects('message').once()
})

HTTP Mock

With Ruby I think this is a common sense, everyone that I know uses VCR. The cassettes are a very cool feature and works like a charm. On the JS side I prefer Nock, in one of my projects I have more examples of it if you wanna take a look.

VCR

VCR.use_cassette('synopsis') do
  uri = URI('http://get.com')
  response = Net::HTTP.get_response(uri)
  expect(response.body).to eq('response')
end

Nock

const payload = { success: true }

nock('http://get.com')
  .get('/get')
  .query(true)
  .reply(200, payload)

Web framework

It's pretty clear at this point that we have a lot of options here. But, let's focus on what some people call "microframeworks". You can use them for simple things or tune with other packages and build bigger projects too. You probably used Sinatra on the red side, if it is the case Express will be a walk in the park for you:

Sinatra

get '/' do
  # show something
end

post '/' do
  # create something
end

Express

app.get('/', (req, res) => {
  res.send({})
})

app.post('/', (req, res) => {
  res.send({})
})

Active Record

Most of the people in the JS world end up using MongoDB, but in a lot of cases the relational database will be better. Yes, they are not hipster databases but it's the true. And if we are talking about databases you will probably need an ORM, Sequelize will have everything that you need:

Sequelize

const User = sequelize.define('user', {
  username: Sequelize.STRING,
  birthday: Sequelize.DATE
})

sequelize.sync().then(() => {
  return User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  })
})

- Ok, but the migrations ?!

Node Postgres

$ db-migrate create add-pets
exports.up = function (db, cb) {
  db.createTable('pets', {
    id: { type: 'int', primaryKey: true },
    name: 'string'
  }, cb);
}

exports.down = function (db, cb) {
  db.dropTable('pets', cb)
}

About the rails new

Well, if you want a full stack framework like Rails I recommend Meteor. It has Node on back end and you can choose between React and Angular on the front end but if you have already choosed your stack and are searching for a genrator, use Yeoman !

You can actually use one the generators they have in the website for popular stacks, yey !

Keeping an open mind

I showed you libraries of the Node world that are similar to the Rails stack to lower your entrance barrier but I advise: do not try to completely reproduce your last environment on the new one. Keep an open mind about new ways to do the same things you do. Give a fair shot for new techs, a lot of people work hard to make it available. See you soon :)