I will assume that you already install nodejs on your machine since sailsjs build on it, otherwise you should see this link and go to how to install nodejs section.
npm -g install sails, for some cases maybe you will need to use
sudo npm -g install sails
- Go to your dev folder and type:
sails new awesomeAPI
- and now you already can run test on your api server
since it's still on development you will be asking something like this: choose 1 for this tutorial, then you can access it from your browser using port 1337 (http://localhost:1337)
Create the API
For this tutorial we will create Restful API for access table user and profile which has one to one association.
- create controller and model for user and profile
sails generate api user
sails generate api profile
it will automatically create a controller and model for you.
- now, we declare what are attributes/columns inside those models, open user model inside
api/models/User.jsthen, we also add attributes on profile model, since it will one-to-one association so we add
useridattribute as a foreignKey of user model
- And now we can use our Restful API, run
add data user using
you should json output like so: next, we add profile on that data which has id=1
- Now you can see your data
and if you want fetch single data, you can do like so:
- if you want to update one of the field in profile table, do it like so:
this one is for delete
And our restful API is ready to use for developing either web or mobile application.