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.
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.
This will create a server running at localhost:1337, and the page may look something like this
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
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
In our case lets say I want to edit user with id 7, we should call
*Delete : *
To delete any record we need to call api as below ,
So to delete user with id 7, we should call
To summarize :