为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

子衿沉夜 2023-05-25 15:38:51
我玩过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,这会是一个合适的解决方案吗?我需要服务器端生成的对象,因为它包含可能不会传送到客户端的凭据。
查看完整描述

2 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

一个粗略的例子,但给出一个想法,将脚本文件嵌入到 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);


查看完整回答
反对 回复 2023-05-25
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

它与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代码中。

查看完整回答
反对 回复 2023-05-25
  • 2 回答
  • 0 关注
  • 152 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号