Introduction
- Node.js® is an open source, cross-platform JavaScript run-time environment and is a frequently used scripting language available for Linux, MacOS, Windows, and other platforms.
- This article explains how to include Node.js® scripts with JS7 job scripts.
- As a prerequisite to executing Node.js® code it is required to install Node.js on the server for which the JS7 Agent is operated.
Invoking Node.js® from Jobs
Unix
- Find the below examples for download (.json upload): run-Node.js-unix.workflow.json
In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that runs Node.js® as the interpreter of the script like this:
Example how run Node.js® script code with a shebang#!/usr/bin/node var name =(process.env.name); var num = parseInt(process.env.num); //print value of variable console.log( name); console.log( num);
Alternatively, the Node.js® script can be executed from a file that has to be located within reach of the JS7 Agent that runs the job:
Example how to run Node.js® script code from a filenode /some/location/JS7-2.5.1/demo_Node.js
Windows
- Find the below examples for download (.json upload): run-Node.js-windows.workflow.json
In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that runs Node.js® as the interpreter of the script like this:
Example how run PowerShell® script code with a shebang replacement@@findstr/v "^@@f.*&" "%~f0"|node.exe -&goto:eof var name =(process.env.name); var num = parseInt(process.env.num); //print value of variable console.log( name); console.log( num);
Explanation:- If you consider this shebang replacement somewhat cryptic then add it to JS7 - Script Includes which are easily referenced from shell jobs, e.g. by using
##!include Node
- The
node.exe
executable as available from the Node.js® installation is executed by the shebang.
- If you consider this shebang replacement somewhat cryptic then add it to JS7 - Script Includes which are easily referenced from shell jobs, e.g. by using
In addition, a Node.js® script can be executed from a file that has to be located with the Agent that runs the job:
Example how to run Node.js® script code from a filenode.exe C:\Users\Documents\demo_Node.js
Passing Variables to subsequent Jobs
Users frequently find a situation when a job creates a result which should be forwarded to subsequent jobs in a workflow.
Download
Download Example for Unix (.json upload): pdwVariablesPassingUnix.workflow.json
Download Example for Windows (.json upload): pdwVariablesPassingWindows.workflow.json
First Job: Write Return Variables
Shell jobs can pass results to subsequent jobs:
- by creating a key/value pair with the syntax:
key=value
. - The key/value pair is appended to a temporary file which is provided by JS7 and that is indicated by the
JS7_RETURN_VALUES
environment variable. - The key provided is the name of the variable which can be used by subsequent jobs.
- If the variable does not yet exist it will be created on-the-fly.
- If the variable exists then the value will be overwritten.
The job script implementation looks like this:
Second Job: Read Variables
Shell jobs access order variables from a mapping to environment variables.
- The JOC Cockpit GUI allows the mapping to be added per job from the right lower corner with the Environment Variables sub-tab.
- The mapping includes free choice of the name of an environment variable which is used in the job script and to assign an existing order variable.
- The spelling of variable names is case-sensitive.
The job script implementation looks like this: