【问题标题】:Get node.js server-side object available in client-side javascript code获取客户端 javascript 代码中可用的 node.js 服务器端对象
【发布时间】:2020-10-20 11:32:23
【问题描述】:

我玩过UiPath Orchestrator package。并且使用安装的 node.js 包连接成功。

无论如何,现在我需要在我的网站中以一种可以从简单的html 网站访问它的方式实现它。

在那里我很难让它运行。这就是我想使用它的方式:

index.html:

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

server.js:(我以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)
  });
}

这行得通。所以测试函数被触发了。

但现在我想获取Orchestrator 对象(如此处所示https://www.npmjs.com/package/uipath-orchestrator)。

如何以最好的方式做到这一点?

也许只是将该对象传递给scripts.js 文件本身?但是如何使用windowglobal 来做到这一点,这会是一个合适的解决方案吗?

我需要服务器端生成的对象,因为它包含可能不会传送到客户端的凭据。

【问题讨论】:

  • 您可以将节点生成的 javascript 嵌入到 html 的脚本文件中。
  • 如何处理对象?一些示例代码?

标签: javascript node.js uipath


【解决方案1】:

一个粗略的例子,但给出一个想法,将脚本文件嵌入到 html 中。 理想情况下,您会使用 express 来加载网页,但这纯粹是为了描述用例。

你可以对结束体标签或结束头标签做同样的事情。

const http = require('http'),
const fs = require('fs');

const html = fs.readFileSync('./file.html'); 
const obj = fs.readFileSync('./script.js'); 

const htmlWithScript = html.replace(/\<\/html\s*\>/,`<script>${obj}</script></html>`);
// const htmlWithScript = `<html><head><script>${obj}</script></head><body> my html stuff ...</body></html>`

http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(htmlWithScript);  
        response.end();  
    }).listen(8000);

【讨论】:

  • 它看起来不错,也许它可以工作。但我无法加载 HTML 文件并为其提供对象。也许你能扩展这个例子并告诉我当 HTML 代码在一个单独的文件中时如何做?
  • 我收到一个错误。但无论如何,这根本行不通。您尝试将源代码包含到 HTML 中。但正如我所说,script.js 文件包含一个凭证。这必须在服务器端执行,如果可能的话,对象必须在客户端执行。该怎么做?
  • 这不是一个完整的解决方案,它是如何解决将脚本文件嵌入到您的html中的问题。提出一个要求更全面的新问题
  • 抱歉,不够清楚。但这是一个要求。当然只需要一个文件并且使用它很容易。但这只是出于安全原因需要返回的对象。我更新了问题。
  • 特别是查看编排器的代码库,这仅适用于节点,因此在 html 的嵌入式脚本中本地有一个实例毫无意义。 github.com/UiPath/orchestrator-nodejs/blob/master/lib/…。你用它来做什么替代品不能做?为什么不在客户端使用 xmlhttprequest 对象、fetch 或 axios 构建类似的逻辑构建自己的库?
【解决方案2】:

UiPath-Orchestrator nodejs完美搭配。

所以只需使用:

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));
});

并将orchestrator 对象提取到您的node.js 代码中。

它还可以与 Orchestrator Cloud 一起使用(如果您没有本地部署),请参阅 here 了解更多信息。

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多