Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-10-20 11:32:23

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.

Questioner
kwoxer
Viewed
0
kwoxer 2021-02-27 18:28:10

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.