Node.js Code
This action makes it possible for you to add custom node code for your workflow. In addition, it comes preloaded with a set of helper functions that eases the process of writing the code.
Helper functions
Helpers, as the name suggests, help you with tasks. Their sole purpose is to 'help' a single function by cleaning the code and making the logic clearer. Helper functions are often very concrete as each one is used in a small section of the code. The various helper functions available for usage within the code action are listed below:
- 1. $require('lib name')
- 2. $store
- 3. $http.get(options)
- 4. $http.post(options)
- 5. $http.put(options)
- 6. $http.del(options)
- 7. $export(err, output)
- 8. $success(output)
- 9. $error(err)
- 10. $log
- 11. Account Store ($accountStore)
- 12. Timers
1. $require('lib name')
Node.Js follows the CommonJS module system, and the built-in 'require' function is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes it, and then proceeds to return the export objects. In order to include any library inside the code, $require helper should be used. It is a wrapper around the native required() in Node.Js.
The following libraries can be used in your code:
1. aws-sdk (v2.0.11)
2. jade (v1.1.5)
3. q (v1.0.1)
4. request (v2.55.0)
5. rest (v1.3.1)
6. underscore (v1.6.0)
7. uuid (v1.4.1)
8. built (v1.2.9)
9. built.io (v2.0.40)
10. phantom (v0.7.2)
11. googleapis (v2.1.7)
12. redis (v0.10.1)
13. mongodb (v2.0.39)
14. lodash (v4.13.0)
15. xlsx (v0.8.0)
16. moment (v2.10.2)
17. soap (v0.9.1)
18. moment-timezone (v0.4.0)
19. htmlparser2 (v3.8.0)
20 async (v1.4.0)
21 pubnub (v3.7.13)
22. metatags (v0.0.8)
23. htmlencode (v0.0.4)
24. xml2js (v0.4.4)
25. ical-toolkit (v1.0.9)
26. postmark (v 1.2.1)
27. nodemailer-smtp-transport (v2.5.0)
28. ws (v1.1.1)
29 dynamics-crm-xrm (v1.0.3)
30. edgegrid (v2.0.7)
31. events
32. crypto
33. path
34. querystring
35. url
36. path
37. buffer
38. events
39. http
40. https
41. assert
42. punycode
43. zlib
44. string_decoder
If you require any other libraries, kindly contact us.
2. $store
The key value ‘store’ is available for storing key-value pairs during workflow execution. It acts like a data store. The data stored is persistent.
2.1 $store.set(key,value,callback)
This method is used to set key-value pair. It is important to note that the ‘key’ should be of string data type, while the ‘value’ can be either json, string, or number.
Example:
$store.set(“abc”, ”hello”, function(err){
if(!err){
$log(‘done’);
}
});
This will set “abc" key with value “hello".
2.2 $store.get(key,callback)
This method is used to retrieve the value of the key stored in $store.
Example:
$store.set(“abc”, ”efg”, function(err){
if(!err){
// sets “abc” = ”efg”
$store.get(“abc”, function(err, data){
// $log(data);
});
}
});
3. $http.get(options)
This is used to perform http get request. It returns a promise. Options should have the following structure:
var options = { url : url, qs : qs, headers : headers}
url – fully qualified uri or a parsed url object from url.parse()
qs – object containing querystring values to be appended to the uri
headers – http headers (default: {})
Example:
$http.get({url : “http://www.google.co.in”})
.then(function(data){
// data
}).fail(function(err){
// http call failed see err
});
4. $http.post(options)
This helper function can be used to perform http post request. It returns a promise. Options should have the following structure:
var options = { url : url, qs : qs, headers : headers,data:data }
url – fully qualified uri or a parsed url object from url.parse()
qs – object containing querystring values to be appended to the uri
headers – http headers (default: {})
data – entity body for PATCH, POST and PUT requests. It must be a JSON-serializable object or String
Example:
$http.post({
url : “http://www.google.co.in”,
data : {name: “pradeep”, location : “virar”}
}).then(function(data){
// data
}).fail(function(err){
// http call failed see err
});
5. $http.put(options)
It is used to perform http put request. It returns a promise. Options should have the following structure:
var options = { url : url, qs : qs, headers : headers,data:data }
url – Fully qualified uri or a parsed url object from url.parse()
qs – object containing querystring values to be appended to the uri
headers – http headers (default: {})
data – entity body for PATCH, POST and PUT requests. It must be a JSON-serializable object or String
Example:
$http.put({
url : “http://www.google.co.in”,
data : {name: “pradeep”, location : “ambernath”}
}).then(function(data){
// data
}).fail(function(err){
// http call failed see err
});
6. $http.del(options)
Use this helprf unction to perform http put request. It returns a promise. Options should have the following structure:
var options = { url : url, qs : qs, headers : headers,data:data }
url – fully qualified uri or a parsed url object from url.parse()
qs – object containing querystring values to be appended to the uri
headers – http headers (default: {})
data – entity body for PATCH, POST and PUT requests. It must be a JSON-serializable object or String
Example:
$http.del({
url : “http://www.google.co.in”,
data : {name: “pradeep”, location : “virar”}
}).then(function(data){
// data
}).fail(function(err){
// http call failed see err
});
7. $export(err, output)
This helper function is used to return control from code action. “err" parameter indicates whether the action has returned an error, while the “output" parameter indicates if it has returned an output. “err" and “output" must be a JSON-serializable object or String.
if err = null, action successfully completed.
if err != null, action has error.
Example:
for error
$export(error);
for output data
$export(null, data);
8. $success(output)
This helper function is used to return control from the code action and tell the system that there was no error. The“output" parameter returns the output. “output" must be a JSON-serializable object or String.
9. $error(err)
It is used to return control from code action and tell the system that there was error during the execution. “err" parameter returns an error. “err" must be a JSON-serializable object or String.
10. $log
$log(data)
This helper function is used to add logs, in particular, workflow bill logs. The only difference between $log() and $log() is that $og() logs data after the action is completed, i.e, after $export() is called, while $log() logs data immediately.
Example:
$log(data);
11. Account Store ($accountStore)
The key value ‘accountStore’ is available for storing key-value pairs during workflow execution. It acts like a data store. The data stored is persistent.
The only difference between Account Store and Workflow Store is that Workflow Store ($store) stores data of a particular workflow Id, so all workflows can get data only set by itself, while Account Store ($accountStore), on the other hand, stores data of a particular account or a user, so it is not tied to any particular workflow. Hence, by using Account Store ($accountStore), any workflow can get data set by other workflows in same user account.
11.1 $accountStore.set (key, value, callback)
This method is used to set key-value pair. It is important to note that the ‘key’ should be of string data type, while the ‘value’ can be either json, string, or number.Example:
$accountStore.set(“abc”, ”hello”, function(err){
if(!err){
$log(‘done’);
}
});
This will set “abc” key with the value “hello”
11.2 $accountStore.get ( key, callback)
This method is used to retrieve the value of the key stored in $accountStore.Example:
$accountStore.set(“abc”, ”efg”, function(err){
if(!err){
// sets “abc” = ”efg”
$store.get(“abc”, function(err, data){
// $log(data);
});
}
});
12. Timers
For more info on Timers, read more on javascript setTimeout, setInterval, clearTimeout and clearInterval.
var timeoutId = $setTimeout(function, milliseconds);
var intervalId = $setInterval(function, milliseconds);
$clearTimeout(timeoutId);
$clearInterval(intervalId);
Output:
-
Node.js Code
-
output any
-