SlideShare a Scribd company logo
Introduction to Sails.js
Document Prepared by
Amit Bidwai
Sr. Software Developer
2018 eTouch Systems. All rights reserved.
Sails.js
Sails.js (or Sails) is a Model-View-Controller (MVC)
web application framework developed on top of
Node.js environment released as free and
open-source software under the MIT License. It's
especially good for building real time features like chat.
2018 eTouch Systems. All rights reserved.
Installation
To install Sails, it is quite simple. The prerequisites
are to have Node.js installed and also npm, which
comes with Node. Then one must issue the
following command in the terminal:
2018 eTouch Systems. All rights reserved.
Command $ sudo npm install sails -g
Create a New Project
In order to create a new Sails project, the following
command is used:
2018 eTouch Systems. All rights reserved.
Command $ sails new myNewProject
Sails will generate a new folder named
myNewProject and add all the necessary files to
have a basic application built. To see what was
generated, just get into the myNewProject folder
Run the Sails server
2018 eTouch Systems. All rights reserved.
Command $ sails lift
Sails's default port is 1337, so if you visit http://localhost:1337 you should get the Sails
default index.html page
File/Folder
Structure Generated
2018 eTouch Systems. All rights reserved.
Now, let's have a look at
what Sails generated for us.
In our myNewProject folder
the following files and
sub-folders were created:
File/Folder Structure Generated -1
2018 eTouch Systems. All rights reserved.
The assets Folder
The assets folder contains subdirectories for the Javascript and CSS files
that should be loaded during runtime. This is the best place to store
auxiliary libraries used by your application.
The public Folder
Contains the files that are publicly available, such as pictures your site
uses, the favicon, etc.
File/Folder Structure Generated -2
2018 eTouch Systems. All rights reserved.
The config Folder
This is one of the important folders. Sails is designed to be flexible. It assumes some standard
conventions, but it also allows the developer to change the way Sails configures the created app
to fit the project's needs. The following is a list of configuration files present in the config folder:
● adapters.js - used to configure the database adapters
● application.js - general settings for the application
● assets.js - asset settings for CSS and JS
● bootstrap.js - code that will be ran before the app launches
● locales - folder containing translations
● policies.js - user rights management configuration
● routes.js - the routes for the system
● views.js - view related settings
The sails.js documentation contains detailed information on each of these folders.
File/Folder Structure Generated -4
2018 eTouch Systems. All rights reserved.
The views Folder
The application's views are stored in this folder. Looking at its contents, we notice that the views
are generated by default as EJS (embedded JavaScript). Also, the views folder contains views for
error handling (404 and 500) and also the layout file (layout.ejs) and the views for the home
controller, which were generated by Sails.
The api Folder . This folder is composed from a buch of sub-folders:
● the adapters folder contains the adapters used by the application to handle database
connections
● the controllers folder contains the application controllers
● the application's models are stored in the models folder
● in the policies folder are stored rules for application user access
● the api services implemented by the app are stored in the services folder
Configure the Application
2018 eTouch Systems. All rights reserved.
So far we have created our application and took a look at what was generated by default, now it's
time to configure the application to make it fit our needs.
General Settings
General settings are stored in the config/application.js file. The configurable options for the
application are:
● application name (appName)
● the port on which the app will listen (port)
● the application environment; can be either development or production (environment)
● the level for the logger, usable to control the size of the log file (log)
Note that by setting the app environment to production, makes Sails bundle and minify the CSS and
JS, which can make it harder to debug.
Configure the Application
2018 eTouch Systems. All rights reserved.
Routes
Application routes are defined in the config/routes.js file. As you'd expect, this file will be the one
that you will most often work with as you add new controllers to the application.
The routes are exported as follows, in the configuration file:
module.exports.routes = {
'/': {
controller: 'home'
},
'/login': {
controller: 'auth',
action: 'login'
},
'post /blog/add': {
controller: 'blog',
action: 'add_post'
},
'/blog/:item': {
controller: blog,
action: find
}
}
Configure the Application
2018 eTouch Systems. All rights reserved.
Models
Models are a representation of the application data stored in a database. Models are defined by
using attributes and associations. For instance, the definition of a Person model might look like
this:
var Person = {
name: 'STRING', age: 'INTEGER', birthDate: 'DATE', phoneNumber: 'STRING', emailAddress: 'STRING'
};
exports = Person;
The communication with the underlying database is done through adapters. Adapters are defined
in api/adapters and are configured in the adapters.js file. At the moment of writing this article, Sails
comes with three adapters: memory, disk and mysql but you can write your own adapter (see the
documentation for details).
Once you have a model defined you can operate on it by creating records, finding records,
updating and destroying records.
Configure the Application
2018 eTouch Systems. All rights reserved.
Controllers
Controllers are placed in api/controllers. A controller is created using the following command:
sails generate controller comment
This command will generate a CommentController object. Actions are defined inside this object.
Actions can also be generated when you issue the generate controller command:
sails generate controller comment create destroy tag like
This will create a Comment controller with actions for create, destroy, tag and like.
Actions receive as parameters the request and the response objects, which can be used for getting
parameters of the URI (the request object) or output in the view (using the response object).
To communicate with the model, the callback of the appropriate action is used. For instance, in the
case of querying a database with find, the following pattern is used to manipulate the model:
Blog.find(id).done(err, blog) {
// blog is the database record with the specified id
console.log(blog.content);
}
Configure the Application
2018 eTouch Systems. All rights reserved.
Views
Views are used to handle the UI of the application. By default, views are handled using EJS, but
any other templating library can be used. How to configure views was discussed previously in the
Configuration chapter.
Views are defined in the /views directory and the templates are defined in the /assests/templates
folder.
There are mainly four types of views:
● server-side views
● view partials
● layout views
● client-side views
Configure the Application
2018 eTouch Systems. All rights reserved.
Server-Side Views
Their job is to display data when a view is requested by the client. Usually the method
res.viewcorresponds to a client with the appropriate view. But if no controller or action exists
for a request, Sails will serve the view in the following fashion: /views/:controller/:action.ejs.
The Layout View
The Layout can be found in /views/layout.ejs. It is used to load the application assets such as
stylesheets or JavaScript libraries.
Client-Side Templates
These are defined in the /assets/templates and are loaded as we saw above.
Configure the Application
2018 eTouch Systems. All rights reserved.
<!DOCTYPE html>
<html>
<head> <title><%- title %></title>
<!-- Viewport mobile tag for sensible mobile support -->
<meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1,
maximum-scale=1&quot;>
<!-- JavaScript and stylesheets from your public folder are included here -->
<%- assets.css() %>
<%- assets.js() %>
</head>
<body>
<%- body %>
<!-- Templates from your view path are included here -->
<%- assets.templateLibrary() %>
</body>
</html>
The lines assets.css() and assets.js() load the CSS and JS assets of our application and the
assets.templateLibrary loads the client templates.
Routes
2018 eTouch Systems. All rights reserved.
We discussed how to configure routes in the Configuration chapter.
There are several conventions that Sails follows when routes are handled:
● if the URL is not specified in the config/routes.js the default route for a URL is
/:controller/:action/:id with the obvious meanings for controller and action and id being
the request parameter derived from the URL.
● if :action is not specified, Sails will redirect to the appropriate action. Out of the box, the
same RESTful route conventions are used as in Backbone.
● if the requested controller/action do not exist, Sails will behave as so:
○ if a view exists, Sails will render that view
○ if a view does not exist, but a model exists, Sails will return the JSON form of that
model
○ if none of the above exist, Sails will respond with a 404
Conclusion
2018 eTouch Systems. All rights reserved.
Now I've barely scratched the surface with what Sails can do, but stay tuned, as I will follow this up
with an in-depth presentation showing you how to build an application, using Sails.
Also keep in mind that Sails is currently under development and constantly changing. So make sure
to check out the documentation to see what's new.
Thank You
eTouch Systems (India) Pvt Ltd
Address: 801-802, A Wing, Teerth Technospace IT Park, Off Mumbai Bangalore Highway,, Behind Mercedes Benz
Showroom, Baner, Pune, Maharashtra 411045
2018 eTouch Systems. All rights reserved.

More Related Content

What's hot

Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
John Lewis
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
Modern webtechnologies
Modern webtechnologiesModern webtechnologies
Modern webtechnologies
Besjan Xhika
 
Rails
RailsRails
RailsSHC
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
zeeshanhanif
 
A Complete Tour of JSF 2
A Complete Tour of JSF 2A Complete Tour of JSF 2
A Complete Tour of JSF 2
Jim Driscoll
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
yuvalb
 
Java Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC FrameworkJava Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC FrameworkGuo Albert
 
Struts Introduction Course
Struts Introduction CourseStruts Introduction Course
Struts Introduction Courseguest764934
 
Introduction to jsf 2
Introduction to jsf 2Introduction to jsf 2
Introduction to jsf 2
yousry ibrahim
 
An IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
An IT Pro Guide to Deploying and Managing SharePoint 2013 AppsAn IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
An IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
Randy Williams
 
Develop iOS and Android apps with SharePoint/Office 365
Develop iOS and Android apps with SharePoint/Office 365Develop iOS and Android apps with SharePoint/Office 365
Develop iOS and Android apps with SharePoint/Office 365
Kashif Imran
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Akhil Mittal
 

What's hot (15)

Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
 
Jsf intro
Jsf introJsf intro
Jsf intro
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
Modern webtechnologies
Modern webtechnologiesModern webtechnologies
Modern webtechnologies
 
Rails
RailsRails
Rails
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
A Complete Tour of JSF 2
A Complete Tour of JSF 2A Complete Tour of JSF 2
A Complete Tour of JSF 2
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Java Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC FrameworkJava Server Faces + Spring MVC Framework
Java Server Faces + Spring MVC Framework
 
Struts Introduction Course
Struts Introduction CourseStruts Introduction Course
Struts Introduction Course
 
Introduction to jsf 2
Introduction to jsf 2Introduction to jsf 2
Introduction to jsf 2
 
An IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
An IT Pro Guide to Deploying and Managing SharePoint 2013 AppsAn IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
An IT Pro Guide to Deploying and Managing SharePoint 2013 Apps
 
Develop iOS and Android apps with SharePoint/Office 365
Develop iOS and Android apps with SharePoint/Office 365Develop iOS and Android apps with SharePoint/Office 365
Develop iOS and Android apps with SharePoint/Office 365
 
Spring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topicsSpring 3.x - Spring MVC - Advanced topics
Spring 3.x - Spring MVC - Advanced topics
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
 

Similar to Introduction to sails.js

Asp.net c# MVC-5 Training-Day-2 of Day-9
Asp.net c# MVC-5 Training-Day-2 of Day-9Asp.net c# MVC-5 Training-Day-2 of Day-9
Asp.net c# MVC-5 Training-Day-2 of Day-9
AHM Pervej Kabir
 
Fame
FameFame
Fame
rpatil82
 
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdfSchema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
seo18
 
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Metamorphosis from Forms to Java:  A technical lead's perspective, part IIMetamorphosis from Forms to Java:  A technical lead's perspective, part II
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Michael Fons
 
Silverlight Development & The Model-View-ViewModel Pattern
Silverlight Development & The Model-View-ViewModel PatternSilverlight Development & The Model-View-ViewModel Pattern
Silverlight Development & The Model-View-ViewModel Pattern
Derek Novavi
 
Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolAdriaan Venter
 
Create an application with ember
Create an application with ember Create an application with ember
Create an application with ember
Chandra Sekar
 
Introduction to ejb and struts framework
Introduction to ejb and struts frameworkIntroduction to ejb and struts framework
Introduction to ejb and struts framework
s4al_com
 
Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)
Kavya Barnadhya Hazarika
 
AGADOS function & feature Chapter-02 biz logic define
AGADOS function & feature Chapter-02 biz logic defineAGADOS function & feature Chapter-02 biz logic define
AGADOS function & feature Chapter-02 biz logic define
Yongkyoo Park
 
Rails review
Rails reviewRails review
Rails review
Alan Hecht
 
.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ
Avijit Shaw
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application Madhuri Kavade
 
Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0
Protect724
 
Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0
Protect724
 
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & FeaturesAEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & Features
Abhinit Bhatnagar
 
Iphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSIphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOS
Kenny Nguyen
 

Similar to Introduction to sails.js (20)

Asp.net c# MVC-5 Training-Day-2 of Day-9
Asp.net c# MVC-5 Training-Day-2 of Day-9Asp.net c# MVC-5 Training-Day-2 of Day-9
Asp.net c# MVC-5 Training-Day-2 of Day-9
 
Fame
FameFame
Fame
 
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdfSchema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
Schema-based multi-tenant architecture using Quarkus &amp; Hibernate-ORM.pdf
 
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Metamorphosis from Forms to Java:  A technical lead's perspective, part IIMetamorphosis from Forms to Java:  A technical lead's perspective, part II
Metamorphosis from Forms to Java: A technical lead's perspective, part II
 
MVC 4
MVC 4MVC 4
MVC 4
 
Silverlight Development & The Model-View-ViewModel Pattern
Silverlight Development & The Model-View-ViewModel PatternSilverlight Development & The Model-View-ViewModel Pattern
Silverlight Development & The Model-View-ViewModel Pattern
 
Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling Tool
 
Create an application with ember
Create an application with ember Create an application with ember
Create an application with ember
 
Introduction to ejb and struts framework
Introduction to ejb and struts frameworkIntroduction to ejb and struts framework
Introduction to ejb and struts framework
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)Android Development : (Android Studio, PHP, XML, MySQL)
Android Development : (Android Studio, PHP, XML, MySQL)
 
AGADOS function & feature Chapter-02 biz logic define
AGADOS function & feature Chapter-02 biz logic defineAGADOS function & feature Chapter-02 biz logic define
AGADOS function & feature Chapter-02 biz logic define
 
Rails review
Rails reviewRails review
Rails review
 
.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application
 
Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0
 
Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0Asset modelimportconn devguide_5.2.1.6190.0
Asset modelimportconn devguide_5.2.1.6190.0
 
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & FeaturesAEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & Features
 
Iphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSIphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOS
 
Synopsis
SynopsisSynopsis
Synopsis
 

Recently uploaded

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 

Recently uploaded (20)

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 

Introduction to sails.js

  • 1. Introduction to Sails.js Document Prepared by Amit Bidwai Sr. Software Developer 2018 eTouch Systems. All rights reserved.
  • 2. Sails.js Sails.js (or Sails) is a Model-View-Controller (MVC) web application framework developed on top of Node.js environment released as free and open-source software under the MIT License. It's especially good for building real time features like chat. 2018 eTouch Systems. All rights reserved.
  • 3. Installation To install Sails, it is quite simple. The prerequisites are to have Node.js installed and also npm, which comes with Node. Then one must issue the following command in the terminal: 2018 eTouch Systems. All rights reserved. Command $ sudo npm install sails -g
  • 4. Create a New Project In order to create a new Sails project, the following command is used: 2018 eTouch Systems. All rights reserved. Command $ sails new myNewProject Sails will generate a new folder named myNewProject and add all the necessary files to have a basic application built. To see what was generated, just get into the myNewProject folder
  • 5. Run the Sails server 2018 eTouch Systems. All rights reserved. Command $ sails lift Sails's default port is 1337, so if you visit http://localhost:1337 you should get the Sails default index.html page
  • 6. File/Folder Structure Generated 2018 eTouch Systems. All rights reserved. Now, let's have a look at what Sails generated for us. In our myNewProject folder the following files and sub-folders were created:
  • 7. File/Folder Structure Generated -1 2018 eTouch Systems. All rights reserved. The assets Folder The assets folder contains subdirectories for the Javascript and CSS files that should be loaded during runtime. This is the best place to store auxiliary libraries used by your application. The public Folder Contains the files that are publicly available, such as pictures your site uses, the favicon, etc.
  • 8. File/Folder Structure Generated -2 2018 eTouch Systems. All rights reserved. The config Folder This is one of the important folders. Sails is designed to be flexible. It assumes some standard conventions, but it also allows the developer to change the way Sails configures the created app to fit the project's needs. The following is a list of configuration files present in the config folder: ● adapters.js - used to configure the database adapters ● application.js - general settings for the application ● assets.js - asset settings for CSS and JS ● bootstrap.js - code that will be ran before the app launches ● locales - folder containing translations ● policies.js - user rights management configuration ● routes.js - the routes for the system ● views.js - view related settings The sails.js documentation contains detailed information on each of these folders.
  • 9. File/Folder Structure Generated -4 2018 eTouch Systems. All rights reserved. The views Folder The application's views are stored in this folder. Looking at its contents, we notice that the views are generated by default as EJS (embedded JavaScript). Also, the views folder contains views for error handling (404 and 500) and also the layout file (layout.ejs) and the views for the home controller, which were generated by Sails. The api Folder . This folder is composed from a buch of sub-folders: ● the adapters folder contains the adapters used by the application to handle database connections ● the controllers folder contains the application controllers ● the application's models are stored in the models folder ● in the policies folder are stored rules for application user access ● the api services implemented by the app are stored in the services folder
  • 10. Configure the Application 2018 eTouch Systems. All rights reserved. So far we have created our application and took a look at what was generated by default, now it's time to configure the application to make it fit our needs. General Settings General settings are stored in the config/application.js file. The configurable options for the application are: ● application name (appName) ● the port on which the app will listen (port) ● the application environment; can be either development or production (environment) ● the level for the logger, usable to control the size of the log file (log) Note that by setting the app environment to production, makes Sails bundle and minify the CSS and JS, which can make it harder to debug.
  • 11. Configure the Application 2018 eTouch Systems. All rights reserved. Routes Application routes are defined in the config/routes.js file. As you'd expect, this file will be the one that you will most often work with as you add new controllers to the application. The routes are exported as follows, in the configuration file: module.exports.routes = { '/': { controller: 'home' }, '/login': { controller: 'auth', action: 'login' }, 'post /blog/add': { controller: 'blog', action: 'add_post' }, '/blog/:item': { controller: blog, action: find } }
  • 12. Configure the Application 2018 eTouch Systems. All rights reserved. Models Models are a representation of the application data stored in a database. Models are defined by using attributes and associations. For instance, the definition of a Person model might look like this: var Person = { name: 'STRING', age: 'INTEGER', birthDate: 'DATE', phoneNumber: 'STRING', emailAddress: 'STRING' }; exports = Person; The communication with the underlying database is done through adapters. Adapters are defined in api/adapters and are configured in the adapters.js file. At the moment of writing this article, Sails comes with three adapters: memory, disk and mysql but you can write your own adapter (see the documentation for details). Once you have a model defined you can operate on it by creating records, finding records, updating and destroying records.
  • 13. Configure the Application 2018 eTouch Systems. All rights reserved. Controllers Controllers are placed in api/controllers. A controller is created using the following command: sails generate controller comment This command will generate a CommentController object. Actions are defined inside this object. Actions can also be generated when you issue the generate controller command: sails generate controller comment create destroy tag like This will create a Comment controller with actions for create, destroy, tag and like. Actions receive as parameters the request and the response objects, which can be used for getting parameters of the URI (the request object) or output in the view (using the response object). To communicate with the model, the callback of the appropriate action is used. For instance, in the case of querying a database with find, the following pattern is used to manipulate the model: Blog.find(id).done(err, blog) { // blog is the database record with the specified id console.log(blog.content); }
  • 14. Configure the Application 2018 eTouch Systems. All rights reserved. Views Views are used to handle the UI of the application. By default, views are handled using EJS, but any other templating library can be used. How to configure views was discussed previously in the Configuration chapter. Views are defined in the /views directory and the templates are defined in the /assests/templates folder. There are mainly four types of views: ● server-side views ● view partials ● layout views ● client-side views
  • 15. Configure the Application 2018 eTouch Systems. All rights reserved. Server-Side Views Their job is to display data when a view is requested by the client. Usually the method res.viewcorresponds to a client with the appropriate view. But if no controller or action exists for a request, Sails will serve the view in the following fashion: /views/:controller/:action.ejs. The Layout View The Layout can be found in /views/layout.ejs. It is used to load the application assets such as stylesheets or JavaScript libraries. Client-Side Templates These are defined in the /assets/templates and are loaded as we saw above.
  • 16. Configure the Application 2018 eTouch Systems. All rights reserved. <!DOCTYPE html> <html> <head> <title><%- title %></title> <!-- Viewport mobile tag for sensible mobile support --> <meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1&quot;> <!-- JavaScript and stylesheets from your public folder are included here --> <%- assets.css() %> <%- assets.js() %> </head> <body> <%- body %> <!-- Templates from your view path are included here --> <%- assets.templateLibrary() %> </body> </html> The lines assets.css() and assets.js() load the CSS and JS assets of our application and the assets.templateLibrary loads the client templates.
  • 17. Routes 2018 eTouch Systems. All rights reserved. We discussed how to configure routes in the Configuration chapter. There are several conventions that Sails follows when routes are handled: ● if the URL is not specified in the config/routes.js the default route for a URL is /:controller/:action/:id with the obvious meanings for controller and action and id being the request parameter derived from the URL. ● if :action is not specified, Sails will redirect to the appropriate action. Out of the box, the same RESTful route conventions are used as in Backbone. ● if the requested controller/action do not exist, Sails will behave as so: ○ if a view exists, Sails will render that view ○ if a view does not exist, but a model exists, Sails will return the JSON form of that model ○ if none of the above exist, Sails will respond with a 404
  • 18. Conclusion 2018 eTouch Systems. All rights reserved. Now I've barely scratched the surface with what Sails can do, but stay tuned, as I will follow this up with an in-depth presentation showing you how to build an application, using Sails. Also keep in mind that Sails is currently under development and constantly changing. So make sure to check out the documentation to see what's new.
  • 19. Thank You eTouch Systems (India) Pvt Ltd Address: 801-802, A Wing, Teerth Technospace IT Park, Off Mumbai Bangalore Highway,, Behind Mercedes Benz Showroom, Baner, Pune, Maharashtra 411045 2018 eTouch Systems. All rights reserved.