REST API's with Sails.js

Today I’m going to create a restful JSON api using sails.js and understand how it works.

Lets create a new Sails app,

sails new jsonapi

This will add the required files to the project, and create a directory named jsonapi.

New lets move to project directory and create the api, lets say I want to create api to manage(Create, Read, Update, Delete) users.
cd jsonapi

and to create the api (Resource)

sails generate api User

The above command will create two files, one inside the api/models directory and other inside the api/controllers directory.

First lets start the sails server to see what I’ve created.

<br></br>
sails lift<br></br>

This will create a server running at localhost:1337, and the page may look something like this

![Sails landing page](http://www.clipular.com/c/5137964219236352.png?k=SabGqu3B8p05e7KJetOtc0AIzB4)
New Sails Server

If you visit http://localhost:1337/user in your browser, you will see an empty json response because there are no users.

You can create a new user by simply typing
http://localhost:1337/user/create?name=Abhishek

Above command will create a new user with name “Abhishek”

Similarly we can create as many users we want.

Now if we open , http://localhost:1337/user we can see all the users we had created.
Something like ,

Lets checkout other options,

*Update : *
To update any record we can use same API as
http://localhost:1337/user/update/?name=

In our case lets say I want to edit user with id 7, we should call
http://localhost:1337/user/update/7?name=Abhishek_3

*Delete : *
To delete any record we need to call api as below ,
http://localhost:1337/user/destroy/

So to delete user with id 7, we should call
http://localhost:1337/user/destory/7

To summarize :

Create : http://localhost:1337/user/create?=
Read : http://localhost:1337/user/
Update : http://localhost:1337/user/update/?name=
Delete : http://localhost:1337/user/destroy/

That’s simple.