Originally I shared this material to Syndicode blog.
If you have read my Rails digests, you wouldn't be surprised with my next find. Sails.js is a Node.js MVC framework inspired by Ruby on Rails that follows convention over configuration principle. Sail.js allows you to quickly build REST APIs, single-page apps, and real-time (WebSockets-based) apps. It makes extensive use of code generators that allow you to build your application with less writing of code — particularly of common code that can be otherwise scaffolded.
Sails.js is the most popular MVC (model–view–controller) framework for Node.js, designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture.
What are Sails.js basic concepts?
- Any database. Sails bundles a powerful ORM, Waterline, which provides a simple data access layer that just works, no matter what database you're using.
- Powerful associations. You can assign different models to different databases, and your associations/joins will still work - even across NoSQL and relational boundaries. Sails has no problem implicitly/automatically joining a MySQL table with a Mongo collection and vice versa.
Auto-generate REST APIs. You can run
sails generate api dentistand you'll get an API that lets you search, paginate, sort, filter, create, destroy, update, and associate dentists. These blueprint actions also work with Websockets and any supported database out of the box.
- No additional code to support WebSockets. Incoming socket messages are automatically compatible with every route in your Sails app, as well as any existing Express routes/middleware. Normalization of parameters, the session, and the streaming interface are all taken care of. The code you write works with WebSockets and HTTP without doing any extra work.
- Declarative, reusable security policies. Simple, reusable middleware functions that run before your controllers and actions. Writing policies encourages encapsulation, that simplifies your business logic and reduces the total amount of code you need. Policies are interchangeable with Express/Connect middleware, and you can plug in popular npm modules like Passport. Also, your policies work for both WebSockets and HTTP automatically.
- Frontend agnostic. Sails.js is compatible with any frontend strategy: Angular, Backbone, iOS/ObjC, Android/Java, Windows Phone, or something else that hasn't been invented yet.
- Flexible asset pipeline. Your entire frontend asset workflow is completely customizable and comes with support for all of the great Grunt modules which are already out there.
- Sails.js is built on Node.js and uses Express for handling HTTP requests.
Main Sails.js features
- it’s built on Express.js
- it has real-time support with WebSockets
- it takes a “convention over configuration” approach
- it has powerful code generation, thanks to Blueprints
- it’s database agnostic thanks to its powerful Waterline ORM/ODM
- it supports multiple data stores in the same project
- it has good documentation.
There are currently a few important cons, such as:
- no support for JOIN query in Waterline
- no support for SQL transactions until Sails v1.0 (in beta at the time of writing)
- until version 1.0, it still uses Express.js v3, which is EOL (end of life)
- development is very slow.
Learn how to generate a new Sails.js project, and then how to create an API by just generating models, adding some attributes then generate controllers here. Also, there you can compare it to Express.js. Because Sails.js sits on top of Express. Its ensemble of small modules works together to provide simplicity, maintainability, and structural conventions to Node.js apps.