将数据从NodeJ传递到HTML

编程入门 行业动态 更新时间:2024-10-07 20:28:26

将<a href=https://www.elefans.com/category/jswz/34/1771445.html style=数据从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

本文发布于:2024-05-07 14:03:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   NodeJ   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!