[英]Pass data from NodeJs to HTML
我正在處理的是一個很長一段時間的問題,我正在使用 Node js 來執行 power-shell 代碼。
我得到了正確的“數據”,但我如何將它傳遞給客戶端(html)?
const express = require('express');
const path = require('path');
var spawn = require("child_process").spawn,child;
const app = express();
app.use('/static', express.static('public'))
app.get('/', function(req, res){
res.sendfile(path.join(__dirname, 'index.html'));
});
//PORT
const port = process.env.PORT || 3000;
app.listen(port);
child = spawn("powershell.exe", ["C:\\Users\\mohammed.alneama\\Desktop\\CCT\\getWinInfo.ps1"]);
child.stdout.on("data",function(data){
console.log( "User: " + data)
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input (edited)
將 shell 執行 output 包裝到promise
,希望它有所幫助
const express = require('express');
let shellData;let shellErr;
let shellPromise = new Promise(function(res,rej){
shellData=res;shellErr=rej;
});
const path = require('path');
var spawn = require("child_process").spawn,child;
const app = express();
app.use('/static', express.static('public'))
app.get('/', function(req, res){
var data = await shellPromise;
//use the express template engine to pass this data to html
res.sendfile(path.join(__dirname, 'index.html'));
});
//PORT
const port = process.env.PORT || 3000;
app.listen(port);
child = spawn("powershell.exe", ["C:\\Users\\mohammed.alneama\\Desktop\\CCT\\getWinInfo.ps1"]);
child.stdout.on("data",function(data){
console.log( "User: " + data);
shellData(data)
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
shellErr(data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input
首先定義一個路由(這會在訪問路由時執行 powershell 腳本):
app.get("/executePowerShellScript", function(req, res) {
res.send("Output of script")
})
然后將您的邏輯移至該路線:
app.get("/executePowerShellScript", function(req, res) {
child = spawn("powershell.exe", ["C:\\Users\\mohammed.alneama\\Desktop\\CCT\\getWinInfo.ps1"])
child.stdout.on("data",function(data){
console.log( "User: " + data)
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input (edited)
})
現在我們需要進行一些調整以使其正常工作:
const express = require('express')
const path = require('path')
const app = express()
app.use('/static', express.static('public'))
app.get('/', function(req, res) {
res.sendfile(path.join(__dirname, 'index.html'))
})
app.get("/executePowerShellScript", function(req, res) {
const spawn = require("child_process").spawn
var powerShellOutput = {stdout: "", stderr: ""}
var child = spawn("powershell.exe", ["C:\\Users\\mohammed.alneama\\Desktop\\CCT\\getWinInfo.ps1"])
child.stdout.on("data", function(data) {
// capture STDOUT data
powerShellOutput.stdout += data.toString()
})
child.stderr.on("data", function(data) {
// capture STDERR data
powerShellOutput.stderr += data.toString()
})
// send response when script is done
child.on("exit", function() {
var response = "<h1>STDOUT</h1>"
response += "<pre>" + powerShellOutput.stdout + "</pre>"
response += "<h2>STDERR</h2>"
response += "<pre>" + powerShellOutput.stderr + "</pre>"
// here we send the response.
res.send(response)
})
child.stdin.end() // end input (edited)
})
//PORT
const port = process.env.PORT || 3000;
app.listen(port)
您只需缺少res.send
,然后您就可以將數據發送到 html。 您可以在此處閱讀更多相關信息。 例子:
app.get('/', function(req, res){
res.sendfile(path.join(__dirname, 'index.html'));
res.send("this will send to hmtl");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.