What is Sails.js?
Sails.js is an MVC web framework for Node.js. Sails.js makes it easy to build custom, enterprise-grade Node.js apps with support for the requirements of modern apps: data-driven APIs with scalable, service-oriented architecture. It’s especially good for building chat, real-time dashboards, or multi-player games.
Easy WebSocket Support: Sails translates incoming socket messages for you, they’re automatically compatible with every route in your Sails app.
Auto-generate REST APIs: Sails comes with blueprints that help jumpstart your app’s back-end without writing any code.
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.
Reusable security policies: Sails provides basic security and role-based access control by default.
Easy to start
To create and start a new Sails.js project, it takes you only 3 commands:
Take a look at localhost:1337
Creating a controller is easy, the sails CLI does all the hard stuff for you. e.g. To create a controller called the user with the methods “index, show, edit, delete”, all you have to do is run the following command:
This will create a file in API/controllers called UserController.js
Creating a model is as easy as creating a controller with Sails.js. To create a model called the user with the fields “name, email, password”, all you have to do is run the following command:
This will create a file in API/models called User.js
Generating a Blueprint API
You can generate the Controller and Model at the same time, also generating a Blueprint API that allows you to visit /user and view the raw JSON representation of the data stored.
This will create both api/controllers/UserController.js and api/models/User.js.
With blueprints enabled you will be able to immediately visit /user/create?name=sails to create a user, and visit /user to see an array of your app’s users. All without writing a single line of code!
This is the result when you hit localhost:1337/user/create?name=sails
In practice, you would want to change or add new methods in the controller, however, the current version of Sails ships with the following blueprint methods:
These can be overridden, though, to disable them or just customise them.
Waterline: SQL/NoSQL Data Mapper (ORM/ODM)
Sails come installed with a powerful ORM/ODM called Waterline, a datastore-agnostic tool that dramatically simplifies interaction with one or more databases. It provides an abstraction layer on top of the underlying database, allowing you to easily query and manipulate your data without writing vendor-specific integration code. Policies in Sails are versatile tools for authorization and access control– they let you allow or deny access to your controllers down to a fine level of granularity. Have a look at pre-defined policy in api/policies/sessionAuth.js:
What this policy does is require a session variable called ‘authenticated’. If not exists, Sails will refuse you access with a message as specified. Edit your config/policies.js as:
Stop your Sails app and run sails lift again. Then hit localhost:1337/user/create. You’ll probably get this:
Above is a quick start with Sails.js. Hope you enjoy and have fun while sailing !!!
For further enquiries, please do not hesitate to contact Savvycom at: