[英]Get array from node.js (server-side)to react app.js (client-side)
[英]Get node.js server-side object available in client-side javascript code
我玩過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
文件本身? 但是如何使用window
或global
來做到這一點,這會是一個合適的解決方案嗎?
我需要服務器端生成的對象,因為它包含可能無法傳送到客戶端的憑據。
一個粗略的例子,但提供一個想法,將腳本文件嵌入到 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);
它與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一起使用(如果您沒有本地部署),請參閱 此處了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.