最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

将数据从NodeJ传递到HTML

网站源码admin13浏览0评论

将数据从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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论