Local Development

Debug Cloud Functions

11min

After creating and developing your application using Parse Cloud Code functions, there’s always room for improvement when it comes to testing and debugging. This guide will show you how to integrate your code editor with Node.js to debug your functions using a local Parse Server instance, simulating the Back4app environment.

Goal

Enable you to debug your Parse Cloud Code locally in your preferred code editor.

1 - Preparing Your Project Files

If you are already hosting your application on Back4app or have set up Cloud Code via the dashboard, your project should follow this structure:

  • cloud Directory: Contains the main.js file where your Cloud Code functions are defined.
  • public Directory: Holds your static content such as HTML and JavaScript files, typically including an index.html file.

If your app is new or not yet deployed, replicate this structure to ensure the local Parse Server runs correctly.

2 - Running Your Parse Server Locally

To start a local instance of the Parse Server:

  1. Navigate to your project directory in the terminal.
  2. Run the following command to launch the server with a test database and your Cloud Code:
parse-server --appId YOUR_APP_ID --clientKey YOUR_CLIENT_KEY --masterKey YOUR_MASTER_KEY --databaseURI mongodb://localhost/test --cloud ./cloud/main.js --verbose
  • Replace the placeholder values (YOUR_APP_ID, etc.) with random values. Avoid using your production keys.
  1. Verify that the server is running by opening http://localhost:1337/parse in your browser. An "unauthorized" error means the server is running but the request lacks authentication keys.

3 - Setting Up and Testing Cloud Code

Ensure all your Cloud Code functions are located in the cloud/main.js file. For example:

main.js


Restart the Parse Server to load the new function:

CTRL+C # to stop the server parse-server --appId ... # rerun the command

Now, test the function using cURL in the terminal:

Curl


If configured correctly, the terminal will display the response "Testing!".

4- Debugging the Code with Node.js

You can use Node.js's debugging features, integrated with Visual Studio Code (or a similar IDE), to debug your functions step by step.

Setting up VS Code

  1. Open the Run and Debug panel on the left sidebar and click Create a launch.json file.
  2. Choose Node.js as the environment.

This creates a basic debug configuration. To enhance it:

  1. Click Add Configuration... and select Node.js: Attach to Process.
  2. Choose the Attach by Process ID action and attach it to the Parse Server's node process.
Document image


Debugging the Code

  1. Open main.js and set a breakpoint on the line return "Testing!"; by clicking to the left of the line number.
  2. Run the same cURL command as before. The debugger will pause execution at the breakpoint.
  3. While paused, inspect environment variable values and the call stack in the debugger panel.

This approach lets you analyze your code’s behavior in detail.

Document image


Conclusion

By following this guide, you’ll be able to debug all aspects of your Parse integration and Cloud Code functions locally, improving your development workflow with Back4app.