Getting started with SailsJS - Part 1
This post is intended to give an introduction to the SailsJS framework and assumes that the reader has some basic understanding of nodeJS and an understanding of the REST API specification.
SailsJS - MVC framework for NodeJS
SailsJS is an MVC framework for NodeJS. The current stable version available is v0.12 with v1.0 on the way to the stable branch. v1.0 is currently in beta. I have been following this framework for some time now and finally got some time on my hands to try out a sample application with this.
The main highlights that I find appealing with this framework are:
- Generators for creating RESTFul APIs.
- Built in support for ORM which makes switching between databases very easy.
- Built in support for socket.io - I am yet to explore this.
To start off, I wanted to write a simple user authentication implementation and then improve it slowly to include the JSON Web Token(JWT) implementation to make authenticated requests for post login requests.
1. Create a new Sails App
Sails comes with generators that allows you to create the required folder structure and the files through the command line. Creating an app is as simple as running the following command:
$ sails new <appname>
Take some time to go through the folder structure and understand how things are organised. You can also take a look at the documentation on the sailsJS web site.
2. Creating the User API
Make sure you are in the project directory. To create a the User model and controller, simply run the following command:
$ sails generate api user
This command creates the User model and the UserController. You can test out the API in any REST client like PostMan and it will work out of the box without even writing a single line of code.
3. Defining the User Model attributes
Let's consider a user sign up scenario which we might have already seen with some other apps. There are 3 basic things that we need to take as inputs from the user:
- Name - First name and Last Name
- Password
So let's model the User Model to contain these attributes. Change the User.js file to add the attributes as given below:
attributes: {
'firstname': {
type: 'string',
required: true
},
'lastname': {
type: 'string'
},
'email': {
type: 'string',
unique: true,
required: true
},
'encryptedPassword': {
type: 'string'
}
}
Notice that there is an attribute called encryptedPassword present in the model. With the current implementation, we will store the password as plain text. Note that the email attribute looks for a unique email which is specified by unique: true. We are also making the first name and email as required fields with the required : true attribute. The validation for this is automatically handled by sails.
Once the changes are done, start the server by running the command:
$ sails lift
Once the server is started, it will display the server URL and the port number in the console. By default, the URL is http://localhost:1337.
For now, let's play around with the app and see if it works fine. You can trigger requests from the rest client and check if the user gets created. Note that the REST routes are automatically created for you so you can just send a POST request to the /user route to create a new user.
In the subsequent posts, we will make some improvements to the app and add some validations and error handling.