【问题标题】:Execute a Node.js child process after clicking a button单击按钮后执行 Node.js 子进程
【发布时间】:2023-03-19 23:52:01
【问题描述】:

目前我在 Node.js 中有一个代码,它调用程序“EnergyPlus”。不幸的是,我必须启动一个外部控制台并“手动”执行 Node.js 文件。但是,我希望能够在我的应用程序前端按下一个按钮来启动“EnergyPlus”加程序。

这是我的 Node.js 文件:

var spawn = require('child_process').spawn,
child = spawn('C:\\EnergyPlusV9-0-1\\EP-Launch.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);

child.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

child.on('close', function (code) {
    console.log('child process exited with code ' + code);
});

有没有办法将此代码集成到按钮中或在单击按钮后执行此代码? 非常感谢您!

【问题讨论】:

  • 我集成了 react 和 webpack,因为我在我的应用程序中使用了它们。
  • 我坚持我的研究,我不知道如何在我的反应应用程序中集成子进程。
  • 那么您将需要一个服务器端部分来执行此代码。您无法从前端直接运行此类代码。
  • 快速服务器正在后台运行。但是我可以或应该如何将节点子进程集成到其中,以及如何以这种方式将 express 服务器与前端连接,以便 EnergyPlus 软件运行?
  • 您可以在您的 express 服务器上配置一个端点(路由)并从响应此路由发出 AJAX 请求时实现这一点,现在您可以在此路由处理函数中调用上层代码并返回响应反应

标签: javascript node.js reactjs webpack energyplus


【解决方案1】:

非常感谢您的帮助! 我找到了解决我的问题的方法。 从头开始,在我的应用程序中,我使用 React 和 Webpack。为了解决我的问题,我按以下方式构建了我的 Server.js 文件(我在其中设置了 Express 行为):

const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
const fs = require("fs")
const spawn = require('child_process').spawn;

// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));
app.use(cors())

// create a GET route
app.get('/express_backend/:filename', (body, res) => {
const f = body.params.filename;

// open EnergyPlus Programm with a specific file which is stored localy
let child = spawn(
'C:\\EnergyPlusV9-0-1\\EP-Launch.exe',
 [process.cwd()+"src/"+ f + ".idf"]
 );

child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

child.on('close', function (code) {
console.log('child process exited with code ' + code);
});

res.send('EnergyPlus is running');
});

// default options
app.use(fileUpload());

//post the uploaded file into the local storage
app.post('/upload', function(req, res) {
  ...
}

// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
let sampleFile = req.files.file;

// Use the mv() method to place the file localy
 fs.writeFile(__dirname + `/upload/${sampleFile.name}`, sampleFile.data,
(err) => {
   ....
  })
});

就像 Nino Filiu 在他的帖子中提到的,我将 child spawn 功能集成到 server.js 中。首先,我用特定文件调用 EP launch.ex,我存储在本地(这个函数不是这个答案的一部分)。 “C:\EnergyPlusV9-0-1\EP-Launch.exe”是 EnergyPlus 的路径。 "[process.cwd()+"src/"+ f + ".idf"]" 帮助 EnergyPlus 直接打开本地存储的字段。 所以关于我的问题的重要一点是 app.get,它是我在 App.js 中触发的。 在 App.js 中,我这样调用 spawn 子进程:

class App extends Component { 

constructor(props){
super(props)
this.state = {filename: null}
};

componentDidMount() {
  ...
};

callBackendAPI = async () => {
  ...
};

//trigger the spawn child function within server.js
startEnergyPlus = (e) =>{
fetch (`http://localhost:5000/express_backend/${this.state.filename}`, {
method: "GET"
});

render(){
 return( 
  <div className="App">
   <button onClick={this.startEnergyPlus}>start app</button>
  </div>
};

就是这样。希望它是清晰和有帮助的。如果没有,请发表评论!

【讨论】:

    【解决方案2】:

    您可以这样做:

    1. 点击客户端按钮,向 Express API 中的特定路径发出请求
    2. 您的 express API 通过启动程序来处理请求

    编写如下所示的代码:

    client.html:

    <button onclick="func()">start app</button>
    <script>
      const func = () => fetch('http://your.api.url/some/path');
    </script>
    

    server.js:

    // imports
    const express = require('express');
    const spawn = require('child_process').spawn;
    
    // create server
    const app = express();
    
    // upon request to the launch path, launch the program
    app.get('/some/path', (req, res) => {
    
      let child = spawn(
        'C:\\EnergyPlusV9-0-1\\EP-Launch.exe',
        ["C:/Windows/System32/Drivers/etc/hosts"]
      );
      // etc (rest of the code you wrote)
    
      // response. You can modify this to send a custom response to your client
      res.send('');
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2021-01-07
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多