Creating your first REST API service using Parse

· Tech · , , , ,

Parse is a great cloud service with lots of features, one them being Parse Cloud Code. Let’s say you have some business logic that you don’t want expose to the public – that’s where Parse Cloud Code comes in. It is basically a dead simple way to create your own API back-end service using nothing but JavaScript. In this example I’ll be using AngularJS to test the API service. Before we start make sure to create a Parse account, at least created one app and to have the latest Parse Console app.

 

1. Open ParseConsole.cmd and enter following command. This command line creates a folder with three subfolders (cloud, config, public). During the creation, you’ll be prompted to enter email, password and to choose app :

parse new FirstCloudCode

2. Go to the cloud folder and open main.js. Here you’ll find an already defined API service called hello. Erease all of the text and type the following code:

Parse.Cloud.define("randomNum", function(request, response) {
var min = parseInt(request.params.min);
var max = parseInt(request.params.max);

var number = Math.floor(Math.random() * (max - min + 1) + min);

response.success(number);
});

This API service receives two parameters (min and max) – generates a random number between min and max number.


3. Once you’ve defined your first Parse Cloude Code, you’ll need to upload/deploy it to your Parse app using the ParseConsole.cmd. Make sure that you’re inside the root folder (see step 1). Enter the following code:

parse deploy

4. Your first REST API service is now ready. Let’s try it out using AngularJS. Before we start with the code, make sure to collect your Parse Application ID and Parse REST ID from your Parse App.

We have created following files with following code:

ParseService.js

</pre>
<pre>parseApp.service("ParseService", function ($http, $q) {
//Values
var ParseURL = "https://api.parse.com/1/functions/randomNum";
var appID = "<INSERT APP ID>";
var restID = "<INSERT REST ID>";
var defer = $q.defer();

//Set up header values
$http.defaults.headers.common['X-Parse-Application-Id'] = appID;
$http.defaults.headers.common['X-Parse-REST-API-Key'] = restID;
$http.defaults.headers.common['Content-Type'] = "application/json";

//Call Parse Cloud Code
this.genRandomNum = function () {
$http.post(ParseURL,
{
"min": "1",
"max": "150"
}
).success(function (result) {
defer.resolve(result);

}).error(function (error) {
defer.reject(error);
});
return defer.promise;
}
});</pre>
<pre>

ParseCtrl.js

</pre>
<pre>parseApp.controller("ParseCtrl", function ($scope, ParseService) {
ParseService.genRandomNum().then(function(results){
$scope.number = results.result;
});
});</pre>
<pre>

Index.html

</pre>
<pre><!doctype html>
<html lang="en" ng-app="parseApp">
<head>
<meta charset="utf-8">
<title>ToDo</title>
</head>

<body ng-controller="ParseCtrl">
<h1>RANDOM NUMBER = {{number}}</h1>

<!--SCRIPTS-->
<script src="lib/angular/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/ParseCtrl.js"></script>
<script src="js/services/ParseService.js"></script>
</body>
</html></pre>
<pre>
Written by almirmesic · · Tech · , , , ,
NextHost your AngularJS app using Parse in 4 simple steps