亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取客戶端 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 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號