数据从NodeJ传递到HTML"/>
将数据从NodeJ传递到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执行输出包装到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
更多推荐
将数据从NodeJ传递到HTML
发布评论