2. Step 2 - Using the Cloud Storage Service Parse.com

The cloud storage service of Parse.com can be used via the REST API of Parse.com, which allows any app that can send HTTP request messages to access the persistent data store on Parse.com. All API access is over HTTPS via the api.parse.com domain. The URL path prefix /1/ indicates that we are currently using version 1 of the API. Using the Parse cloud storage service REST API for our web application we need five functionalities to handle the Book Object, as shown in Table 3.1, “The REST API of Parse.com's cloud storage service”:

Table 3.1. The REST API of Parse.com's cloud storage service

Request URL path HTTP method Meaning
/1/classes/<className> POST Creating an object
/1/classes/<className>/<objectId> GET Retrieving an object
/1/classes/<className>/<objectId> PUT Updating an object
/1/classes/<className>/<objectId> DELETE Deleting an object
/1/classes/<className> GET Retrieving all objects (and other queries)

Notice that an objectId is automatically created by the Parse storage service when a new object is saved. In fact, Parse does not only create an objectId, but also the two time stamp values createdAt and updatedAt of type Date.

For being able to access the Parse cloud storage service we need to set up a user account on the Parse.com website Then go to "Dashboard" and "Create a new App". On the app's "Settings" page you can find its "Application ID" and "REST API Key" using the tab "Keys". Authentication is done via HTTP headers, so we need to provide these keys as the values of corresponding HTTP header fields, like so:

headers: {
  'X-Parse-Aplication-Id': 'Your_Application_ID',
  'X-Parse-REST-API-Key': 'Your_REST_API_Key'

The header field X-Parse-Application-Id identifies which application you are accessing, and X-Parse-REST-API-Key authenticates the endpoint. When we send GET, POST, PUT or DELETE request messages to Parse.com, these two headers have to be included.

The format of a request message body for POST and PUT, as well as the format of all response message bodies, is JSON. In general, sending data to a (remote) back-end server from a JavaScript front-end program is done with asynchronous remote procedure calls using JavaScript's XML HTTP Request (XHR) API. Since such a remote procedure call can result either in a reply message confirming the successful completion of the call or indicating an error state, we generally need to specify two methods as the arguments for calling the remote procedure asynchronously: the first method success() is invoked when the remote procedure call has been successfully completed, and the second one error() when it failed.

AngularJS provides the $http 'service' for sending HTTP request messages (and receiving their response messages) via JavaScript's XHR API. The $http method takes a single argument that is used to generate HTTP request messages and returns a promise with two $http specific methods: success() and error(). The general pattern for a $http send/receive operation is the following:

  headers: {...},
  method: 'GET|POST|...',
  url: 'aRequestUrl'
  .success( function () {...})
  .error( function () {...});

We can add the required Parse authentication headers as default headers in publicLibraryApp.run (in js/app.js), so we don't need to add them to each request. Notice that the $http service has different default headers for different request types: Content-Type: application/json for POST and PUT, and Accept: application/json, text/plain, */* for all request types.