Message:

Subscribe rss
Blog categories
All | Backendless features
Feature 116: Local debugging of custom business logic using CodeRunner
May 6, 2015 by markpiller
Spread the love

Previously I described how to use the Backendless Console to generate custom business logic code. In this post I will describe one of the most amazing features in Backendless – an ability to debug custom server-side code on the developer computer before deploying it to the cloud. It would be very helpful for you to go through the previous feature to establish the surrounding context.

Once the code is generated, you can use the Download button to get a project archive (zip) with all the source code. In addition to the code the archive also contains a special command line utility which you can use to run the custom code locally. The trick of the local execution is the code inject itself into the API processing chain. This happens despite the fact that the API invocation is handled in the cloud, but the custom code runs on your computer. To put things in perspective, see the diagram below:backendless incocation chain - Feature 116: Local debugging of custom business logic using CodeRunner

The green boxes of the “before” and “after” handlers is where the custom code resides. When you run CodeRunner, your custom code will be executed on your local computer.

Suppose the custom code is added for the user registration event (the Register API call). The logic in the custom code is to restrict users from using the @gmail.com, @hotmail.com and @yahoo.com email addresses. I generated an event handler for the User Service’s Register event. The complete event handler code looks like this:

If you compile the code as a part of the generated project downloaded from the Backendless Console and run it with CodeRunner (read about running custom business logic with CodeRunner), you will see the following output:

You can confirm that the event handler is deployed by navigating to the Debug tab on the Business Logic screen in the Backendless Console:
event handler deployed - Feature 116: Local debugging of custom business logic using CodeRunner

It is important to understand that even though CodeRunner is running locally on your own machine, it is plugged into the API invocation chain for your Backendless app. This means the API requests for which you created an event handler will be routed to your computer by Backendless. To see it in action, open another command prompt window and run the following command:

Make sure to replace the YOUR-APP-ID  and YOUR-REST-SECRET-KEY  values with the specific IDs from your Backendless backend.

The command sends a user registration API request. Since the “email” property ends with “@gmail.com”, the custom business logic which runs on your own machine will handle and short-circuit the request. The default logic for user registration of the User Service will not be invoked in this case at all. You will see the following error in the command prompt where you issued the REST command:

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+