Local Development

Debug Cloud Functions

12min

Cloud Code Local Debugging

Introduction

After creating and developing your application using Parse Cloud Code functions, there is always room for improvement regarding testing and debugging on the app. In this guide, you will learn how to integrate your code editor to Node.js to debug your function execution using a local Parse Server instance emulating a live Back4App environment.

All the information in this guide can also be seen in much more detail in the video below.



Prerequisites

To complete this tutorial, you will need:

Goal

Debug locally your Parse Cloud Function code in your favorite code editor.

1 - Preparing your app files and structure

If you are already hosting your application on Back4App or did the cloud code setup via Dashboard, your project (or cloud code functions) should have the following structure:

  • a public directory containing your static content like HTML and JS files, usually containing a single index.html file in it;
  • a cloud directory containing the main.js file in which your cloud functions are declared.

When following this guide with a fresh app or an undeployed one, replicate the same structure in it, so your local Parse Server will find the files as well.

2 - Running your local Parse Server

Inside your project directory, run the following command on your terminal to start your local Parse Server using a test database and your cloud code. Since this Parse instance is running locally, you should create random placeholder values instead of using your production keys retrieved from your app on Back4App:

parse-server --appId YOUR_APP_ID --clientKey YOUR_CLIENT_KEY --masterKey YOUR_MASTER_KEY --databaseURI mongodb://localhost/test --cloud ./cloud/main.js --verbose

To check if your server was successfully started, you can go to your browser and open “http://localhost:1337/parse” (you can find this URL at the last line of the previous command output). Parse will render an unauthorized error since this simple GET request had no authentication keys in it.

Document image


If you want to use this local setup to test your regular application queries and functions, replace the Parse keys with the ones informed when starting the server. The same goes for your Parse server URL, which should be the local one as well. Using your application with this setup will consume local-only data and generate local logs that will help gauge how your app is running.

3 - Setting up and testing your Cloud Code

Before continuing this step, make sure that all your cloud code functions are located in the cloud/main.js file. Add the following one to it or create a new file containing it:

cloud/main.js


Restart your Parse Server by quitting the previous process and running the same command once again, so you can be sure that the new cloud function was deployed to it as well.

Let’s now make a CURL request to the new function, which is the simplest and lightest way to communicate with your server, and check if everything is working. Paste the following request in your terminal:

Curl


You should get a simple result message on your terminal, which will also be present on your Parse Server process output and inside the log files contained in the logs directory that should have been created automatically by the server.

Document image


These logs can help you monitor the responses and basic behavior of your server, but they aren’t enough when debugging a long or complex cloud function.

4 - Debugging a Cloud Function

Let’s now use the Node.js debugging feature integrated into your code editor. For this guide, we will be using VS Code, but most of the recent IDEs also have the same support for it. More details on Node.js debugging can be read on the official docs.

On your left sidebar, click on the Run and Debug item and then on create a launch.json file, selecting Node.js as the environment option.

Document image


This will create an example debug configuration that will work right out of the box, but we can improve by adding a new configuration through the Add Configuration... button and selecting Node.js: Attach to process.

Now we can use this configuration to attach the debugger to our local Parse Server node process, so select the Attach by Process ID action on the top of the “RUN AND DEBUG” sidebar, click on the green arrow, and then click on the node process related to Parse.

Go back to your main.js file and create a breakpoint on the return line of the debugTest cloud function by clicking on the red circle to the left of the line number. Make the same CURL request to your server as before and the debugger should pause the function execution at that line.

Document image


While the application is paused, you can check the current environment variables values and the current call stack of methods. This is an extremely powerful tool to determine if the data is being properly treated inside your code and to understand more about how the server handles requests.

Conclusion

After this guide, you should be able to debug every aspect of your Parse integration and also your Cloud Code function behavior, improving your development experience.