获取客户端 javascript 代码中可用的 node.js 服务器端对象

我玩过UiPath Orchestrator 包。并且连接与安装的 node.js 包一起工作。

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

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

索引.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) => {

...

});

脚本.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文件本身?但是如何用windowor做到这一点global,这会是一个合适的解决方案吗?


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


子衿沉夜
浏览 153回答 2
2回答

慕姐4208626

一个粗略的例子,但给出一个想法,将脚本文件嵌入到 html 中。理想情况下,您会使用 express 加载网页,但这纯粹是为了描述用例。您可以对结束主体标签或结束头部标签执行相同的操作。const http = require('http'),const fs = require('fs');const html = fs.readFileSync('./file.html');&nbsp;const obj = fs.readFileSync('./script.js');&nbsp;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) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; response.writeHeader(200, {"Content-Type": "text/html"});&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; response.write(htmlWithScript);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; response.end();&nbsp;&nbsp;&nbsp; &nbsp; }).listen(8000);

拉莫斯之舞

它与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代码中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript