Basics of Sails Js — Node Js MVC Framework

Vinita Yadav
Geek Culture
Published in
3 min readMay 7, 2021

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

app.js

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

API

This directory contains the MVC structure of the app

api →controllers → helpers → models → policies

controllers

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

Here are three ways to create a controllers in sails js

1.

 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

2.

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

3.

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.

models

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

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

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

config/routes.js

Now time to test all these api we just created

Inside config directory go to routes.js

config/routes.js

Paste the following code below view route

Now you can test your route from postman at this request

localhost:1337/test/create

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 :)

--

--

Vinita Yadav
Geek Culture

I write articles to share my experiences with technologies :)