How to use NPM modules in Codeless logic in API Services, Event Handlers and Timers

With the introduction of the Custom Code Codeless block, it is now possible to use functionality from NodeJS modules in Codeless logic. This recipe demonstrates the process of importing an NPM module and using it in a Codeless API Service.

A sceptic reader may point out that using the Custom Code in Codeless is an oxymoron. Technically, it is, however, any code you place inside of Custom Code would be seen in your overall logic as one Codeless block, so I’d continue to maintain the position that it is still Codeless :wink:

Back to the recipe… to demonstrate the usage of NodeJS npm module in a Codeless API service, we will use the Fibonacci npm module. The module provides the functionality of calculating the Fibonacci number for the provided number of iterations. The usefulness of the functionality is rather narrow, however, the approach for deploying the code into Backendless and using it in Cloud Code is universally applicable to any other node module.

Prerequisites:

You must have the NPM installed locally. When you run the npm command in a Terminal/Command Prompt window, you should be getting a response that indicates that npm is installed. For example:fib — -bash — 172×37 2021-10-06 07-12-01

Using NPM modules in Backendless Business Logic (Cloud Code) requires installing all the modules locally first.

  1. Open a command prompt window and create (or navigate to) a directory where you will download all the required modules. In my case, the directory is called /fib
  2. Run the following command:
    npm install --save fibonacci
    
    The --save argument is important as it instructs npm to save downloaded modules in the current directory rather than global/shared/main npm storage on your computer.
  3. Once the command above completes, your directory structure should look like as shown below:
    fib 2021-10-06 07-18-50
  4. The node_modules directory contains the actual modules we’re interested in. Open Backendless Console and navigate to the Files section of your app. Locate the servercode directory. Drag and drop the node_modules directory from your local computer (the directory is created in step #3) into Root/servercode directory in console. Once the directories and files are uploaded, your servercode directory in console should look like this:
    Files Browser - servercode - CustomCodeJS - Backendless 2021-10-06 07-39-43
    when navigating to the node_modules directory in Backendless Console, you should see the following:
    node_modules - CustomCodeJS - Backendless 2021-10-06 07-40-32
  5. Now that the modules have been deployed, switch to the Business Logic section and create a new Codeless API service.
  6. Click SAVE and you will be prompted to create a method for your service:
  7. Ope the method’s logic for editing. Drag the Custom Code block into the method’s logic and connect it with the return connector:
  8. Click the Add a new argument field and type in it and press Enter. This will create an argument for the Custom Code block called `it. Paste the following code into the editor:
    const fibonacci = require ('fibonacci');
    return fibonacci.iterate (it);
    
    Notice the first line imports the fibonacci module. The module is imported from the node_modules directory you deployed to your Backendless app in one of the steps above.
  9. Click the SAVE AND CLOSE button. Connect the Method Argument iterations to your Custom Code block:
  10. Finally click the DEPLOY MODEL button to deploy your API Service:

When you get a confirmation saying the service has been deployed, you can switch to the API SERVICES tab and invoke the service. Don’t forget to enter a value for the iterations argument:

Now you have a Codeless API services that use an NPM module with custom code.

4 Likes