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 on how stressful, unorganised and difficult to maintain it will be building 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 fullstack MVC web application framework built on top of Express 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’s 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 light weight, 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 of 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 a 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 similar fashion, so switching from one to the other was really 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 User
model should use a MongoDB database while the Product
model should use a PostgreSQL database in your Sails application.
It gives developers the opportunity 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 less 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 writing codes.
Maintenance
With Sails, you have a standardised code base across modules since developers must follow the same folder and file structure throughout every Sails projects making it easy to maintain. Where as 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 development experience easier.
Begin your Sails journey today; Join the Sailscast community.
See you there!