Difference between SailsJS and ExpressJS (SailsJs vs ExpressJs)

Difference between SailsJS and ExpressJS (SailsJs vs ExpressJs)

When i first heard about sails, i googled about its difference with express, but i did not found any suitable answer, so now that i have worked on many projects using Sails over Express, let me tell you guys the actual difference you would come across when transitioning from Express to Sails.

Do learn Express before migrating to Sails

This is the most important thing which no one else will tell you. Sails is an awesome framework but if you do not know how routing works, or what an ORM is, what are the various HTTP methods, then you will not be able to get the magic behind Sails.

More than that, you would need to better understand these concepts in order to become a great Web Developer. So if you do not know much about these concepts, try googling them for a day or two.

I will tell you about these three main components while working on Sails and Express.

Routing

While Express router is cool and works like a charm, you still need to write code for each and every route, if you want to use routes within routes(or subroutes) you would need to use Regular Expressions in order to nest them properly.

You would need to do something like this in Express:

This is probably our main file or the root file of your Express project:

Then this might reside somewhere inside your modules:

This would be your controller file:

So, as you can see, this is what you will have to do if you follow Express best practices.

If your are using sails, just write:

and you will get a model(schema) and a controller file for it. Now just define your schema in the model file(ORM used here is waterline in place of mongoose used in traditional Express apps) and your CRUD apis will be generated automatically for you. You will not be able to see the code for your apis because its all generic code which sails adjusts on your behalf.

Test your APIs in Postman Client and see the magic yourself. The best part is that these apis are also Restful(Again, if you do not know what REST and Restful are, please google). So we have:

  1. CREATE-POST
  2. READ-GET
  3. UPDATE-PUT
  4. DELETE-DELETE

ORM

ORM used in Sails is Waterline while people use Mongoose when working with Express. Transitioning from mongoose to waterline will be a walk in the park as both work in a similar fashion, waterline uses BlueBird promises library which is also good to learn.

The main advantage Waterline gives you is that you are not limited to just MongoDB, ie. you can use any Database with it(SQL as well as NoSQL) without changing your code. Adapters for various databases are available on npm and they could be configured to be used with Waterline.

However, one drawback of Waterline is that you are not allowed to define embedded objects/documents as you can do in Mongoose. But support of the three main relationships ie. One to Many, Many to Many and One to One can be implemented beautifully with waterline as compared to mongoose.

Write Less Boilerplate with Sails

Everything in sails is pre installed and configured for you, if you want to enable/disable any feature, you can easily do it in the config files, sails follows Convention Over Configuration.

Maintenance of Large Projects

Since sails follows the COC principle, source code across all the projects are very similar because the developers have to follow the same folder structures and file structures in every sails project. This makes your code base standardised across multiple modules.

So i think by now i would recommend SailsJs to anyone who is reading this post. I will write more details soon, ask me anything in comments, i will be glad to help.