Utilizing built-in and external modules
While developing your functions, you may wish to take advantage of an existing module within your code. Web Service Component Functions can call upon modules to simplify the development process. These include: modules that are built-in to Node, and third-party modules available from code repositories such as npm
. As an example, you may wish to call upon the moment
javascript module to simplify working with dates in your code. To achieve this, you need to ensure that your Formbird environment is setup with the required modules.
Running in a sandbox
Web service component functions run within a sandbox on the server-side. The sandbox provides an isolated environment to run your code. The sandbox design intentionally restricts what built-in and external modules you can access. In order to utilize a Node built-in module or external module within your code, you will first need to white list it in Formbird. White listed modules are injected into the sandbox and hence available to use.
Warning: The more modules you allow, the more fragile your sandbox becomes. Each module should be evaluated to determine their appropriateness to be included. For example, modules such as fs
should be avoided as they can provide access to a server's file system which introduces obvious security risks.
Please contact your Formbird administrator to request / perform this setup in your environment.
Enable built-in modules
- Update the Formbird
configuration
document with the required module name under theserverConfiguration.vm2.builtinModuleWhitelist
key.
For example, to enable Node's built-in assert
module, update the builtinModuleWhitelist
array value as follows:
json
"serverConfiguration" : {
"vm2" : {
"builtinModuleWhitelist" : [
"assert"
]
}
}
Tip: A listing of available Node built-in modules: https://www.w3schools.com/nodejs/ref_modules.asp
Enable external modules
- Download the required module from your chosen code repository.
-
Copy the module to a new folder under the
./server/vendor/
folder of the Formbird install. -
Update the Formbird
configuration
document with the required module name under theserverConfiguration.vm2.externalModuleWhitelist
key.
For example, to enable the moment
module:
-
Download
moment
usingnpm
or from it's website https://momentjs.com/. -
Copy the file
moment.js
to the following server location:./server/vendor/moment/
The file should be appear as follows: ./server/vendor/moment/moment.js
- Update the
externalModuleWhitelist
array value as follows:
json
"serverConfiguration" : {
"vm2" : {
"externalModuleWhitelist" : [
"moment"
]
}
}
Calling modules from your code
To call a white listed built-in module from your code, require
the module by name, and use it as required, for example:
const assert = require('assert');
assert.equal(10, 10);
To call a white listed external module from your code, require
the module by relative path and name, and use it as required, for example:
var moment = require("./server/vendor/moment/moment");
var dateVal = moment().format();