Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 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:

    Code Block
    languagejs
    titleExample how run Node.js® script code with a shebang replacement
    linenumberstrue
    @@setlocal enabledelayedexpansion & @@findstr/v \"^@@[fs].*&\" \"%~f0\" | node.exe -NonInteractive -Command - & exit !errorlevel!/b&
    
    var  var name = (process.env.name);
    var num = parseInt(process.env.num);
    
    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 <name-of-script-include>
      • The node.exe executable as available from the Node.js® installation is executed by the shebang.

  • In addition, a Node.js® script can be executed from a file located with the JS7 Agent that implements the job:

    Code Block
    languagejs
    titleExample how to run Node.js® script from a file
    linenumberstrue
    node.exe C:\Users\Documents\sample_Node.js

...

Code Block
languagejs
titleExample of a Windows Shell job passing variables
linenumberstrue
collapsetrue
@@setlocal enabledelayedexpansion & @@findstr/v \"^@@f^@@[fs].*&\" \"%~f0\" | node.exe -&goto:eof & exit !errorlevel!/b&

const fs = require('fs');
 
// fetch the env.* variables
var name = (process.env.name);
var num = parseInt(process.env.num);
 
// print value of variable   
console.log(name);
console.log(num);

// modify the values
var num1 = num + num;
console.log(num1);

var name1 = name + " This is JS7 ";
console.log(name1);

// pass results from key/value pairs that are appended to a temporary file provided by JS7
fs.appendFile(process.env.JS7_RETURN_VALUES, 'num1=' + num1 + '\n', (err) => {});
fs.appendFile(process.env.JS7_RETURN_VALUES, 'name1=' + name1 + '\n', (err) => {});

...

Code Block
languagejs
titleExample of a Windows Shell job reading variables
linenumberstrue
collapsetrue
@@setlocal enabledelayedexpansion & @@findstr/v \"^@@f^@@[fs].*&\" \"%~f0\" | node.exe -&goto:eof & exit !errorlevel!/b&

var name1 = (process.env.name1);
var num1 = parseInt(process.env.num1);

// print value of variable   
console.log(name1);
console.log(num1);