Hands On Sails.js, build Node.js webapps in minutes.

Installing Sails

npm install sails -g
npm install sails-mysql --save

Let’s get started

sails new myproject
sails lift
localhost:1337 

Create your backend API

sails generate api users

Blueprints

Database adapter

attributes: {
firstname: {type: 'string'},
lastname: {type: 'string'}
},
connection:’someMysqlServer’

Page design

Let’s interact with the DB from the frontend

<a href=”/users/displayAll”>Display users</a>
module.exports = {
displayAll:function(request, response){
Users.find({}).exec(function(err, users){
if(err){res.send(500, 'Query failed');}
response.view('displayAll', {users: users});
});
}
};
<table>
<thead>
<tr>
<th>ID</td>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<% users.forEach(function(user){ %>
<tr>
<td> <%= user.id %> </td>
<td> <%= user.firstname %> </td>
<td> <%= user.lastname %> </td>
</tr>
<% }) %>
</tbody>
</table>

Edit API methods or create custom ones

/lib/hooks/blueprints/actions/
'GET /helloworld': {blueprint: 'helloworld'}

Conclusion

--

--

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