Setting up Cloud Code

If you already created a Back4App app (for example, through the website or apps API) and want to set up Cloud Code for the app, b4a new can still help you.

Open your terminal and type the next command

$ b4a new
Would you like to create a new app, or add Cloud Code to an existing app?
Type "(n)ew" or "(e)xisting": n
Please choose a name for your Parse app.
Note that this name will appear on the Back4App website,
but it does not have to be the same as your mobile app's public name.
Name: CLI-Back4App
Awesome! Now it's time to set up some Cloud Code for the app: "CLI-Back4App",
Next we will create a directory to hold your Cloud Code.
Please enter the name to use for this directory,
or hit ENTER to use "CLI-Back4App" as the directory name.

You can either set up a blank project or create a sample Cloud Code project.
Please type “(b)lank” if you wish to setup a blank project, otherwise press ENTER:
Successfully configured email for current project to: ${ACCOUNT_EMAIL}

Your Cloud Code has been created at ${CUR_DIR}/appName

This includes a “Hello world” cloud function, so once you deploy, you can test that it works, with the printed curl command.

Next, you might want to deploy this code with:

$ b4a deploy
Uploading source files
Uploading recent changes to scripts...
The following files will be uploaded:
/Users/alysson/CLI-Back4app/cloud/main.js
Uploading recent changes to hosting...
The following files will be uploaded:
/Users/alysson/CLI-Back4app/public/index.html
Finished uploading files
New release is named v1 (using Parse JavaScript SDK v2.2.25)

Once deployed you can test that it works by running:

curl -X POST \

-H "X-Parse-Application-Id: ${APPLICATION_ID}" \

-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \

-H "Content-Type: application/json" \ -d '{}' \

https://parseapi.back4app.com/functions/hello

You can see the result os our function

{"result":"Hello world!"}

Note: This flow assumes that you already set up account keys. See also multiple account keys, if you are developing on apps belonging to different Parse accounts.

The appCreatedDirectory directory looks like:

 

captura-de-tela-2017-01-11-as-18-30-58

.parse.local and .parse.project are config files that store application info, and project level info (for instance, Javascript SDK version), respectively. The cloud directory stores your Cloud Code, and the public directory stores any static content that you want to host on Parse. In the cloud directory, you’ll typically just be editing main.js, which stores all of your Cloud Code functions.

For now, just check that these files were created successfully. If you’re using source control, you can check all of these files in.

We recommend using source control to check in all of these files. If you’re not already set up with source control, try this tutorial from GitHub.

The same code can be deployed to multiple different applications. This is useful so that you can have separate development and production applications. Then you test the code on a development application before launching it in production.

The first application that is added (by the new command) will be the default application for all command line operations. All commands except new take an optional app name that the command will be performed on.