简体   繁体   English

将数据从 NodeJs 传递到 HTML

[英]Pass data from NodeJs to HTML

I am dealing with is an issue for a long time, I am using Node js to execute power-shell code.我正在处理的是一个很长一段时间的问题,我正在使用 Node js 来执行 power-shell 代码。

I am getting the correct "data" but how I can pass it to the client(html)?我得到了正确的“数据”,但我如何将它传递给客户端(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)

Wrap the shell execution output to a promise , hope it helps将 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

First define a route (this will execute the powershell script when the route is accessed):首先定义一个路由(这会在访问路由时执行 powershell 脚本):

app.get("/executePowerShellScript", function(req, res) {
    res.send("Output of script")
})

Then move your logic into that route:然后将您的逻辑移至该路线:

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)
})

Now we need to make a few adjustments to make it work:现在我们需要进行一些调整以使其正常工作:

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)

You only missing res.send and then you can send data to html.您只需缺少res.send ,然后您就可以将数据发送到 html。 you can read more about that in here .您可以在此处阅读更多相关信息。 Example:例子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM