6. RESTful API
GET /articles/
GET /articles/?$order_by=title&$page=4&$per_page=10
POST /articles/
GET /articles/104
PUT /articles/104
PATCH /articles/104
DELETE /articles/104
GET /articles/$metadata
7. Example Request
$ curl -X POST -H ‘Content-Type: application/json’ -d ‘{ “title”: “My first article”,
“body”: “Hello, World!” }’ http://localhost:8080/articles/
{
“id”: 4,
“title”: “My first article”,
“body”: “Hello, World!”,
“created_at”: “2014-06-05T03:14:15.927Z”,
“updated_at”: “2014-06-05T03:14:15.927Z”
}
8. Example Request
$ curl -X POST -H ‘Content-Type: application/json’ -d ‘{ “title”: “Bad”, “body”:
“Too short title” }’ http://localhost:8080/articles/
{
“success”: false,
“error”: “Property ‘title’ should be at least 5 characters.”
}
10. Queries
queries:
MostDiscussed:
“SELECT * FROM articles
INNER JOIN (SELECT article_id, count(id) AS
count FROM comments GROUP BY article_id) AS c
ON articles.id = c.count
ORDER BY c.count DESC”
API: GET /articles/MostDiscussed/?$page=4
11. Scripts
var email = require( ‘emailjs’ );
exports.onCreate = function( req, article, cb ) {
var address = req.auth.user.email;
var subject = “You have created an article”;
var message = “You have created an article with title
”” + article.title + “”.”;
email.send( address, { subject: subject, message:
message } );
cb();
};