- Print
- DarkLight
- PDF
This node provides a convenient way to add custom functionality to your Corezoid process by allowing you to access, modify, and create task parameters using either JavaScript or Erlang (see Picture 1).
Picture 1. Code node task flow
The Code node is specifically intended for handling simple code snippets. For more complex coding tasks, we recommend the Git Call node as a better alternative.
Although the Code node is highly efficient in terms of speed, you may encounter a timeout error if your code is computationally intensive and takes an extended period to execute, or if it includes an infinite loop.
If your code requires extra processing power, you can create a dedicated microservice and call it from Corezoid. If you need further assistance, don't hesitate to contact us for custom solutions that will better fit your needs.
Node Settings
Code Editor
JavaScript
JavaScript runs on V8 engine v8.1.97
Task parameters can be found within the data object, and you can access them using dot notation, like this:
data.parameter_name
The Code node provides access to several helpful JavaScript libraries. However, unlike with the Git Call node, you cannot import additional libraries.
List of supported JavaScript libraries:
Available CryptoJS methods:
Hashing:
Ciphers:
Encoders:
Erlang
Erlang/OTP 24.3
When working with Erlang, ensure your code adheres to a basic program structure, which includes:
- A module statement.
- An export function that accepts one parameter.
- A function definition with data as a parameter.
Your code should look like the following example:
-module(node).
-export([main/1]).
main(Data) -> [{<<"param">>, <<"Hello World!!!">>} | Data].
Here Data is the task object, and param is one of it's parameters.
Verify code - checks and alerts you of syntax errors.
Format code - cleans up your code formatting.
Other
Alert if the number of tasks in the node queue reaches the following number - If this option is selected, a new condition will be created within the node. It will allow you to monitor whether the number of tasks in the node exceeds the specified threshold.
Maximum interval, for which the task stays in the node before being forwarded - the amount of time a task is allowed to be in this node. Can be specified in seconds, minutes, hours, or days. It can also be specified as a unixtime function. This value must be at least 30 seconds.
Examples
Working with date/time using moment.js
require("libs/moment.js");
let datetime = moment().format('MMMM Do YYYY, h:mm a');
data.message = "Hi " + data.name + ". Today is " + datetime +". What a great time to learn Corezoid!" ;
In this example, we first import the moment.js library, which is one of the libraries available in Corezoid's Code node. We then use the library's moment()
method to obtain the current date and time, and the format()
method to convert it into a string. This value is assigned to a local variable.
Next, we create a string using the value of the name parameter from the data
object and the value of the datetime
variable we generated. We assign this string to data.message
, which creates a new task parameter with the message
key and the value of the string.
Keep in mind that the data object stores the task parameters. This means that name
was an existing task parameter, and message will be added as a new one. However, datetime
is a local variable and will not be part of the task; it only exists temporarily while the node processes the task.
Moment-timezone
require("libs/moment-timezone.js");
data.date = moment().tz('Europe/Kiev').format("DD-MM-YYYY HH:mm:ss");
Although the name is similar, this library is distinct from the moment.js library mentioned in the previous example. This library's purpose is to convert and format dates for various time zones.
Like before, we use the moment()
method to obtain the current time and the format()
method to define the desired format. However, now we utilize the tz()
method to change the time zone to Europe/Kiev.
Hashing
MD5
require("libs/md5.js");
data.md5 = CryptoJS.MD5(data.variable).toString();
data.variable - task parameter that will be hashed.
data.md5 - task parameter that will store the hash.
SHA-1
require("libs/sha1.js");
data.sha1 = CryptoJS.SHA1(data.variable).toString();
data.variable - task parameter that will be hashed.
data.sha1 - task parameter that will store the hash.
SHA-512
require("libs/sha512.js");
data.sha512 = CryptoJS.SHA512(data.variable).toString();
data.variable - task parameter that will be hashed.
data.sha512 - task parameter that will store the hash.
HMAC SHA-1
require("libs/hmac-sha1.js");
var hash = CryptoJS.HmacSHA1(data.message, data.secret);
data.hashInHex = hash.toString(CryptoJS.enc.Hex);
data.message - task parameter that will be hashed.
data.secret - task parameter that stores the secret.
data.hashInHex - task parameter that will store the hash as a hexadecimal string.
HMAC SHA-256
require("libs/hmac-sha256.js");
var hash = CryptoJS.HmacSHA256(data.message, data.secret);
data.hashInHex = hash.toString(CryptoJS.enc.Hex);
data.message - task parameter that will be hashed.
data.secret - task parameter that stores the secret.
data.hashInHex - task parameter that will store the hash as a hexadecimal string.
Ciphers
AES
Encrypting
require("libs/aes.js");
data.encrypted = CryptoJS.AES.encrypt(data.message, data.password).toString();
data.message - task parameter that will be encrypted.
data.password - task parameter that contains the password.
data.encrypted - task parameter that will store the encrypted string.
Decrypting
require("libs/aes.js");
data.decrypted = CryptoJS.AES.decrypt(data.encrypted, data.password).toString(CryptoJS.enc.Utf8);
data.encrypted - task parameter that will be decrypted.
data.password - task parameter that contains the password.
data.decrypted - task parameter that will store the decrypted string.
DES
Encrypting
require("libs/tripledes.js");
data.encrypted = CryptoJS.DES.encrypt(data.message, data.password).toString();
data.message - task parameter that will be encrypted.
data.password - task parameter that contains the password.
data.encrypted - task parameter that will store the encrypted string.
Decrypting
require("libs/tripledes.js");
data.decrypted = CryptoJS.DES.decrypt(data.encrypted, data.password).toString(CryptoJS.enc.Utf8);
data.encrypted - task parameter that will be decrypted.
data.password - task parameter that contains the password.
data.decrypted - task parameter that will store the decrypted string.
Triple DES
Encrypting
require("libs/tripledes.js");
data.encrypted = CryptoJS.TripleDES.encrypt(data.message, data.password).toString();
data.message - task parameter that will be encrypted.
data.password - task parameter that contains the password.
data.encrypted - task parameter that will store the encrypted string.
Decrypting
require("libs/tripledes.js");
data.decrypted = CryptoJS.TripleDES.decrypt(data.encrypted, data.password).toString(CryptoJS.enc.Utf8);
data.encrypted - task parameter that will be decrypted.
data.password - task parameter that contains the password.
data.decrypted - task parameter that will store the decrypted string.
Rabbit
Encrypting
require("libs/rabbit.js");
data.encrypted = CryptoJS.Rabbit.encrypt(data.message, data.password).toString();
data.message - task parameter that will be encrypted.
data.password - task parameter that contains the password.
data.encrypted - task parameter that will store the encrypted string.
Decrypting
require("libs/rabbit.js");
data.decrypted = CryptoJS.Rabbit.decrypt(data.encrypted, data.password).toString(CryptoJS.enc.Utf8);
data.encrypted - task parameter that will be decrypted.
data.password - task parameter that contains the password.
data.decrypted - task parameter that will store the decrypted string.
RC4
Encrypting
require("libs/rc4.js");
data.encrypted = CryptoJS.RC4.encrypt(data.message, data.password).toString();
data.message - task parameter that will be encrypted.
data.password - task parameter that contains the password.
data.encrypted - task parameter that will store the encrypted string.
Decrypting
require("libs/rc4.js");
data.decrypted = CryptoJS.RC4.decrypt(data.encrypted, data.password).toString(CryptoJS.enc.Utf8);
data.encrypted - task parameter that will be decrypted.
data.password - task parameter that contains the password.
data.decrypted - task parameter that will store the decrypted string.
Encoders
Base64
Encoding
require("libs/base64.js");
var wordArray = CryptoJS.enc.Utf8.parse(data.variable);
data.base64 = CryptoJS.enc.Base64.stringify(wordArray);
data.variable - task parameter that will be encoded.
wordArray - local variable that stores a wordArray representation of data.variable
.
data.base64 - task parameter that will store the encoded string.
Decoding
require("libs/base64.js");
var words = CryptoJS.enc.Base64.parse(data.base64);
data.parsedStr = words.toString(CryptoJS.enc.Utf8);
data.base64 - task parameter that will be decoded.
words -local variable that stores a wordArray representation of data.base64
.
data.parsedStr - task parameter that will store the decoded string.
Error handling & troubleshooting
When an error occurs in the node, a task goes to the auxiliary Condition output node (Picture 2):
Picture 2. Code node's auxiliary output node
This auxiliary Condition node is used for storing error parameters. With that, the following fields are added to the initial task depending on the error and node type:
Error parameter name | Parameter description |
---|---|
__conveyor_node_type_return_type_error__ | Error type: hardware (system error), software (error in a node logic/settings) |
__conveyor_node_type_return_type_tag__ | Error tag (see the *-marked errors in Error tag column of the table below) |
__conveyor_node_type_return_type_description__ | Error description in human-readable language. Can be static or dynamic |
Problem/Error tag | Cause | Solution |
---|---|---|
Failing to access task parameters | The code references objects or object properties that are absent from the task. | Define the missing objects or object properties within the Code node or adjust the code to remove any references to them. |
A task parameter is referenced without using the “data” object. | Add a call to the data object in front of the name of a task parameter. For example, data.amount = 1 is correct, while amount = 1 is not. | |
Task parameters are gone | The object “data'' was assigned a new value in the Code node. | “data” is a special object used by Corezoid to keep track of task parameters. Because of this, access to the task parameters will be lost if it is modified. You should avoid reassigning this object, and instead, use another name for your variables. |
*"code_timeout" error | Code has been running for too long. | Check if your program contains an infinite loop. |
*"code_executing_error" error | Your code contains an error. | Debug your code. |
*"code_return_format_error" error | Your code has a return statement that doesn't belong in a function and returns a value that isn't an object. The Code node wraps your code into a function, so an unexpected return statement can cause the program to end abruptly. | Remove the return statement to resolve the error. |
*the node errors that are listed with the exact error tag (conveyor_node name_return_type_tag).