Message:

Subscribe rss
Blog categories
All | Backendless features
Developing an API Service with JavaScript and additional NPM modules
December 29, 2017 by markpiller

This post describes the process of developing an API Service in Backendless with JavaScript. You will learn:

  • how configure your development environment
  • include NPM dependencies
  • run the service in the debug mode and test it using the Backendless console
  • deploy the service to Backendless

The service you will develop in this guide will provide APIs for controlling a LIFX wi-fi enabled light bulb. This very service is used in the article describing how to integrate Amazon Alexa with an IoT device using Backendless. This article references IntelliJ IDEA as the IDE, however, it is not required, you can use any code editor. Make sure to create a Backendless account and create an app in Backendless Cloud (the free tier or the trial option will be plenty to proceed).

Setting Up the Backendless Development Environment

  1. Login to your Backendless account and click the Business Logic icon:
    bizlogin icon - Developing an API Service with JavaScript and additional NPM modules
  2. Switch to the CODING section and select JS and click the download icon. Backendless will generate a JS project for your app:
    coding js download - Developing an API Service with JavaScript and additional NPM modules
  3. Extract the downloaded zip file, it will contain the minimally required set of  the project files to start development with Backendless:
    downloaded project structure - Developing an API Service with JavaScript and additional NPM modules
  4. Open a command prompt window and switch to the root directory of the extracted project. Run the following command, it will install all required dependencies, including Backendless CodeRunner.

At this point, your development environment is fully configured. Any code you add to the project can be debugged locally and deployed to the Backendless Cloud.

Adding Service Dependency

The service we will develop will use the node-lifx NPM module. To install the module, run the following command from the project’s root directory:

After the command runs, the package.json file is updated to reflect the node-lfix dependency (the name property will contain the name of your app):

Developing Service

  1. Create a file in the services directory, you can name it lifxservice.js  (the name of the file is not that important, the extension must be .js ).
  2. Copy and paste the following source code listing into the file:

There are a few things to point out in the code:

  • The service class (lines 22-64) is a plain JS class without any special Backendless dependencies.
  • The class is registered to become an API service on line 66.
  • The implementation uses the node-lifx  npm module which is imported on line 8.
  • Class methods are marked with the async  keyword to indicate that they return JS Promise object.
  • Class methods use the API provided by the node-lifx  module (lines 31, 43, 58). The API is asynchronous and returns a promise. Notice how the methods return and resolve the promise (lines 30/32, 42/44, 57/59).

Running the API Service with Debug CodeRunner

To test your API service, run the following command from the project’s root directory:

CodeRunner inspects the project’s code and registers it with Backendless. You will see the following program output:

The following two lines confirm that the service code has been recognized by CodeRunner:

Return to Backendless Console and select the API SERVICES section under Business Logic. You will see the service in the SERVICES IN DEBUG section:

service in debug - Developing an API Service with JavaScript and additional NPM modules

To invoke the API operations/methods for a service in debug, click the method name on the left, enter the operation’s body and click the INVOKE button. As a shortcut for entering the method’s body, you can click the Schema of body section and Backendless Console will copy the value into the Body area.

Once a method is invoked, you will see the method’s return value in the RESPONSE tab:

invocation result - Developing an API Service with JavaScript and additional NPM modules

You will also see the following activity in the command prompt window where you launched Debug CodeRunner with npm run debug. This information is helpful to confirm that the CodeRunner received an invocation request from Backendless Cloud:

As you can see, API services can be debugged on your local computer while being plugged in to Backendless Cloud. Invocation requests can be submitted to Backendless Cloud and will be routed to your local machine.

Publishing the API Service to Backendless

To publish the service to Backendless, run the following command:

When publishing any business logic to Backendless with the command shown above, CodeRunner will display the following message and the prompt:

What this says is the code you’re publishing will be deployed to the "default"  deployment model. If you already have some code in the Cloud in that deployment model, it will be replaced completely by the code you’re publishing. To change the model, you can modify the coderunner.json json file (see the "model"  property in the file).

To continue, type in Y  and press Enter. CodeRunner will proceed with deploying the code to Backendless Cloud:

Once the code is deployed, you will be able to see the service in Backendless Console in the DEPLOYED SERVICES section on the API SERVICES screen.

deployed service - Developing an API Service with JavaScript and additional NPM modules

Service operations can be invoked the same way as for the services in debug. For any API service registered or running in Backendless (both debug and deployed), you can generate the client-side API by clicking the Download Client SDK icon. The downloaded SDK includes the complete code in the selected language which can be used to perform invocations of the service from the client-side:

download client sdk - Developing an API Service with JavaScript and additional NPM modules

Share this post
Tweet about this on TwitterShare on FacebookGoogle+