Hi there! 👋
This article discusses my experience with Sails and Express which are two Node.js backend frameworks and also which to go for when building your Node.js backend applications.
Initially, I was hesitant to learn Sails until it became necessary for a Sailscasts community project. Since then, I have discovered a lot about the Sails framework and have come to love it.
Background Story - How it all started with Node.js
My introduction to backend JavaScript was using Express to write APIs.
While using Express, I didn’t bother with software architectural patterns like the MVC pattern and instead, I had all of my routes, models, configs, etc, written in a file.
I know that sounds very chaotic thinking about it now especially after I have grasped the importance of the MVC architecture. However, back then I didn’t have a clue how stressful, unorganized and difficult to maintain it will be to build APIs all in a single file like I was doing.
Moving forward to when I came across MVC, oh boy! That was another learning curve because I had gotten comfortable with building my APIs in a file and now I had to start picking the pieces, understanding what each of these things meant. It was a lot!
What is MVC?
You may be wondering what MVC is.
Well, MVC also known as Model View Controller is a software architectural pattern based on three components namely:
Model - handles the business logic and data of the app,
View - handles format and layout of data, and
Controller - handles requests and updates between the two
Learning about MVC was a game-changer for me. Although it was a bit challenging to grasp the concept in practicality until I learned about Sails
I like to say that Sails solidified my understanding of MVC. It may have been because I spent more time learning it and less time learning MVC as a concept, but it certainly helped cement my knowledge of MVC.
Sails
Sails or Sails.js is a full-stack MVC web application framework built on top of Express and designed to make it easier to build custom, enterprise-grade applications.
It emulates the MVC pattern of frameworks like Ruby on Rails but with support for the requirements of modern applications: data-driven APIs with a scalable, service-oriented architecture.
This means that if you want to build heavy applications that are data-driven such as chat applications that’ll require real-time event-based updates, messaging capacities, various dashboards, streaming analytics, multi-player games or applications, Sails is the best way to go.
Express
Express on the other hand is a lightweight, minimalist framework that provides a minimal set of features for building web applications.
With Express, you can structure your application in one file using any directory structure and glue components together without a lot of restrictions.
Thoughts on Sails and Express
Now that we know what both Sails and Express are, I want to share my thoughts on both frameworks based on some features.
Routes
Express router works just fine, but you still have to write code for every route you run, but Sails comes with dedicated files for routes and route handlers.
For quick prototyping, with Sails blueprint feature all you have to do is define your schema in a model file and you’ll be able to generate your CRUD endpoints automatically. All that heavy lifting is being taken care of by Sails.
For more on how Sails compare to Express in terms of routing, check out this comparison video in the Sailscasts YouTube channel
ORM
Sails has a built-in ORM(Object Relational Mapper) called Waterline. However, in Express, you need to go grab an ORM like Mongoose(if you are working with MongoDB as your database) or perhaps Prisma and do the entire setup yourself.
After having used Mongoose with Express, as I started using Sails, I discovered that Waterline and Mongoose work in a similar fashion, so switching from one to the other was easy for me.
The biggest advantage Waterline has is that it doesn’t limit you to just MongoDB, which means you can use any database(both SQL and NoSQL).
With Waterline, you do not have to depend on a single database for your application and it is possible to use any of these databases without making changes to the code by just configuring Waterline.
Super cool right? Well, that’s not all!
Sails via Waterline also gives you the freedom and opportunity to assign different models to different databases, and your associations/joins will still work. For example, you can say your The user
model should use a MongoDB database while the The product
model should use a PostgreSQL database in your Sails application.
It allows developers to quickly generate API routes and actions based on your application design. You could read more about it here: Blueprints
Not to forget that you also write fewer boilerplate with Sails compared to Express. Although people say they like the flexibility Express gives but Sails just makes it easy and convenient allowing you to focus on logic rather than writing codes.
Maintenance
With Sails, you have a standardized code base across modules since developers must follow the same folder and file structure throughout every Sails project making it easy to maintain. Whereas in express, it could be a hassle maintaining large projects with it.
Which Framework is Right for Your Project?
This decision comes down to the type of project you are looking to build.
If you’re building a small to medium-sized application, and want more control on picking the libraries to use on your project, Express can be considered a good choice. However, if you’re building a large, complex application with many models and controllers, Sails is unquestionably the best choice suited for this and it will handle every of your project’s needs easily!
I hope this information sheds some light on why you should choose Sails; and with no bias in mind, I would easily recommend Sails.js to anyone. I mean this framework comes with amazing features making the development experience easier.
Begin your Sails journey today; Join the Sailscast community.
See you there!