What is Sails.js ?

Sails is a Javascript framework designed to resemble the MVC architecture from frameworks like Ruby on Rails. It makes the process of building Node.js apps easier, especially APIs, single page apps and realtime features, like chat.

What we will Build ?

In this part we will build a rest api for blog.

  • Each user can create many blog posts.There is a one to many relationship between user and post.
  • Each category has many posts,One to many relationship between category and post

 

 

Getting Started with Sails cli

To install sails.js you must have installed Node.js on your machine.

You can create a new project with sails new projectName

We are going to create sails project without frontend code because we will build frontend part in Angular2 . I will discuss more about Angular2 in part2 .

App Structure

Click here to learn more about app structure of Sails.js App Structure

Connecting mongodb to sails.js

we are going to use mongodb as a database.You can use any database to build your backend rest api. Sails provide more than 30 adapters like sails-mysql, sails-mongodb, sails-redis. Click here to read more about database adapters. Database Adapters

To install sails-mongo adapter

Add connection configuration for mongodb in config/connections.js

Make sure you run mongodb from your terminal or command prompt. In first terminal you will have to run this command.

Open another terminal and run this command

Your mongodb will be running on port 27017. Create a database by running this command

Add mongodb connection to models.js

Set default connection in development.js

Creating models

Create a new file User.js in api/models

I have created one to many relationship between users and posts. I have defined posts collection in user model because we can populate all the posts for each user by using user.find().populate(‘posts’) . It will return all the posts for each user.

Create a new file Post.js in api/models

When we will create a new posts we also need to add id of category model and user model

Create a new file Category.js in api/models

We can also populate all the posts for each category

Defining Routes

Create new User

To create new user . we need a new UserController.js file in api/controllers

User.create() will return a promise. Click here to learn more about promises Promises .If you want to test this route you can use postman to test your rest-api. Postman is a chrome extension. If you have google chrome you can install postman very easily.

We have defined an action method for each route. Let us create new PostController.js in api/controllers . This time I will create a new controller by using sails cli

To create PostController.js run this command

Create a new post

Update Post

Sails cli already created a dummy or empty update method for us. Refactor existing update() in api/controllers/PostController.js

 

Delete Post

Refactor existing delete() in PostController.js

 

FindAll Posts

To get all posts you need to write code in PostController.findAll() in api/controllers/PostController.js

 

Find Single Post

To get post by id you need to write code in PostController.findOne()

In the next part we will create a frontend in Angular2 & Angular4.

Source Code