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
inCodeless
is an oxymoron. Technically, it is, however, any code you place inside ofCustom Code
would be seen in your overall logic as oneCodeless
block, 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.
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:
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
/fib
- Run the following command:
Thenpm install --save fibonacci
--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. - Once the command above completes, your directory structure should look like as shown below:
- The
node_modules
directory contains the actual modules we’re interested in. Open Backendless Console and navigate to theFiles
section of your app. Locate theservercode
directory. Drag and drop thenode_modules
directory from your local computer (the directory is created in step #3) intoRoot/servercode
directory in console. Once the directories and files are uploaded, yourservercode
directory in console should look like this:
when navigating to thenode_modules
directory 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.
- Click
SAVE
and you will be prompted to create a method for your service:
- Ope the method’s logic for editing. Drag the
Custom Code
block into the method’s logic and connect it with thereturn
connector:
- Click the
Add a new argument
field and type init
and press Enter. This will create an argument for theCustom Code
block called `it. Paste the following code into the editor:
Notice the first line imports theconst fibonacci = require ('fibonacci'); return fibonacci.iterate (it);
fibonacci
module. The module is imported from thenode_modules
directory you deployed to your Backendless app in one of the steps above. - Click the
SAVE AND CLOSE
button. Connect theMethod Argument iterations
to yourCustom Code
block:
- 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.