Basics of Sails Js — Node Js MVC Framework

Vinita Yadav
May 7 · 3 min read

Let's start with understanding what is MVC

M - Model (Involves Databases, and rules)

V - View (It is the design and visual aspect)

C - Controller (It connects model and view via logic)

MVC is a software design pattern that helps us to structure the code into models, controllers, and views hence improving the code structure and readability.

Sails JS is an MVC framework for nodeJS, it is built on the top of node js and express Js so all that is applicable in NodeJS and express will work with sails JS.

[ Here is the installation guide to sails js ]

Here is how a sails project is structured


It lies in the root of the folder, it is the main file to start the whole app


This directory contains the MVC structure of the app

api →controllers → helpers → models → policies


Inside controllers we write the logic to connect models with view.

Here are three ways to create a controllers in sails js


 sails generate controller test

This creates a testController.js file inside controllers directory, inside of which you can write your functions, like this

If you have a complex project it is advised to create stand-alone files for every function rather than grouping them all in one, next we see how to create stand-alone controllers


sails generate action test/create

This creates a file controller/test/create.js. The function is intialized with multiple fields like inputs, exits and fn. You can write you create logic inside


sails generate action test/create — no-actions2

This method will create a stand-alone file like in step 2 but with standard node js functions with access to req and res object.


Here is how we create a model

sails generate model test

Here you can define your database schema, api/models/Test.js, the Test schema will now be globally accessible inside your app, you need not require it everytime you interact with database.


Helpers contain globally accessible file, you helpers to create files/functions which are required by multiple controllers.

sails generate helper test

Now this helper can be called from anywhere using

await sails.helpers.test(…, …);


Policies are basically like express middlewares where you can write code for authorization and authentication. Create a file called test.js inside api/policies and paste the following code inside it

you can now apply this middleware(policy) in you create.js.

Go to config/policies.js and add the follwing lines of code


Now time to test all these api we just created

Inside config directory go to routes.js


Paste the following code below view route

Now you can test your route from postman at this request


You should see ‘created’ message!! and inside the console you should your message from policies/test.js

inside test policy

This should be enough to get you started with the sails js app.

See you next time [sails js crud app]

Thank You :)

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store