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
Codelessis an oxymoron. Technically, it is, however, any code you place inside of
Custom Codewould be seen in your overall logic as one
Codelessblock, so I’d continue to maintain the position that it is still Codeless
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.
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:
Using NPM modules in Backendless Business Logic (Cloud Code) requires installing all the modules locally first.
- 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
- Run the following command:
npm install --save fibonacci
--saveargument is important as it instructs npm to save downloaded modules in the current directory rather than global/shared/main npm storage on your computer.
- Once the command above completes, your directory structure should look like as shown below:
node_modulesdirectory contains the actual modules we’re interested in. Open Backendless Console and navigate to the
Filessection of your app. Locate the
servercodedirectory. Drag and drop the
node_modulesdirectory from your local computer (the directory is created in step #3) into
Root/servercodedirectory in console. Once the directories and files are uploaded, your
servercodedirectory in console should look like this:
when navigating to the
node_modulesdirectory in Backendless Console, you should see the following:
- Now that the modules have been deployed, switch to the Business Logic section and create a new Codeless API service.
SAVEand you will be prompted to create a method for your service:
- Ope the method’s logic for editing. Drag the
Custom Codeblock into the method’s logic and connect it with the
- Click the
Add a new argumentfield and type in
itand press Enter. This will create an argument for the
Custom Codeblock called `it. Paste the following code into the editor:
Notice the first line imports the
const fibonacci = require ('fibonacci'); return fibonacci.iterate (it);
fibonaccimodule. The module is imported from the
node_modulesdirectory you deployed to your Backendless app in one of the steps above.
- Click the
SAVE AND CLOSEbutton. Connect the
Method Argument iterationsto your
- Finally click the
DEPLOY MODELbutton 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.