Search code examples
javascriptnode.jsuipath

Get node.js server-side object available in client-side javascript code


I played around with UiPath Orchestrator package. And the connection worked out with the installed node.js package.

Anyway now I need to implement it in my website in a way where I get access it from a simple html site.

There I struggle a bit with getting it to run. This is how I would like to use it:

index.html:

<html>
  ...
  <button onclick="test()">Do something</button>  
  ...
  <script src="scripts.js"></script>
  ...
</html>

server.js: (I start with node server.js)

var http = require('http'),
    fs = require('fs');
const port = 6543;
const path = require('path');
const server = http.createServer((req, res) => {
  let filePath = path.join(
      __dirname,
      req.url === "/" ? "index.html" : req.url
  );
  let extName = path.extname(filePath);
  let contentType = 'text/html';
  switch (extName) {
      case '.js':
          contentType = 'text/javascript';
          break;
  }
  console.log(`File path: ${filePath}`);
  console.log(`Content-Type: ${contentType}`);
  res.writeHead(200, {'Content-Type': contentType});
  const readStream = fs.createReadStream(filePath);
  readStream.pipe(res);
});
server.listen(port, (err) => {
...
});

scripts.js:

function test() {
  ...
  // get the orch object here without defining it here as it contains credentials
  var orchestratorInstance = new Orchestrator({
    tenancyName: string (optional),
    usernameOrEmailAddress: string (required),
    password: string (required),
    hostname: string (required),
    isSecure: boolean (optional, default=true),
    port: integer (optional, [1..65535], default={443,80} depending on isSecure),
    invalidCertificate: boolean (optional, default=false),
    connectionPool: number (optional, 0=unlimited, default=1)
  });
}

This works. So the test function is fired.

But now I would like to get the Orchestrator object (like shown here https://www.npmjs.com/package/uipath-orchestrator).

How to do it in the best way?

Maybe just pass-through that object to the scripts.js file itself? But how to do that with window or global and would that be a proper solution?

I need the server-side generated object as it contains credentials that may not be delivered to client-side.


Solution

  • It works perfectly together with UiPath-Orchestrator nodejs.

    So just use:

    var util = require('util');
    var Orchestrator = require('uipath-orchestrator');
    var orchestrator = new Orchestrator({
         tenancyName: 'test',           // The Orchestrator Tenancy
         usernameOrEmailAddress: 'xxx',// The Orchestrator login
         password: 'yyy',               // The Orchestrator password
         hostname: 'host.company.com', // The instance hostname
         isSecure: true,                // optional (defaults to true)
         port: 443, // optional (defaults to 80 or 443 based on isSecure)
         invalidCertificate: false, // optional (defaults to false)
         connectionPool: 5 // options, 0=unlimited (defaults to 1)
    });
    var apiPath = '/odata/Users';
    var apiQuery = {};
    orchestrator.get(apiPath, apiQuery, function (err, data) {
        if (err) {
            console.error('Error: ' + err);
        }
        console.log('Data: ' + util.inspect(data));
    });
    

    and extract the orchestrator object to your node.js code.

    It also works together with Orchestrator Cloud (if you don't have the on-prem), see here for more info.