子进程在node.js中运行时如何先完成加载

时间:2019-04-22 09:34:25

标签: javascript node.js express server child-process

我对node.js很陌生。目前,当我评估http://localhost:4000/testjar时,我会运行一个子进程来执行jar文件。子进程完成运行后,需要花费一些时间来加载页面,然后显示标准输出。

无论如何,我可以在等待子进程运行的同时先完成页面的加载,然后在子进程运行完毕后将响应发送给客户端。因为我不希望页面加载太长时间。

现在的过程是

    browser loading -> start child process -> wait child process 
-> child process finish ->server send response -> browser finish loading

反正有

browser loading -> start child process -> browser finish loading and display pending for output 
-> wait child process -> child process finish ->server send response

这是我的testjar.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {      
    var exec = require('child_process').exec, child;

    child = exec('java -jar /home/java/testinput.jar',
        function (error, stdout, stderr){
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            res.send(stdout);

            if(error !== null){
                console.log('exec error: ' + error);
            }
    });
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

您可以为此使用socket.io

npm install --save socket.io

app.js

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

app.use(express.static('public'));

function startProcess(callback) {
    setTimeout(function() {
        callback("I am done!");
    }, 3000);
    /*
    var exec = require('child_process').exec, child;
    child = exec('java -jar /home/java/testinput.jar',
        function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            callback(stdout);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
        }
    );
    */
}

app.get('/start_process', function(req, res){
    console.log("got start_process request from socketId = " + req.query.socketId);
    io.to(req.query.socketId).emit('message', "process started");
    startProcess(function(message) {
        console.log('process finished, send message');
        io.to(req.query.socketId).emit('message', message);
    });  
    res.send('OK');
});

io.on('connection', function(socket){
    console.log('socket.io connected');
    socket.on('disconnect', function(){
        console.log('socket.io disconnected');
    });
});

server.listen(3000, function() {
    console.log('listening on *:3000');
});

public/index.html

<!doctype html>
<html>
  <head>
    <title>Socket.IO Demo</title>
  </head>
  <body>
    <button id="startButton">start process</button>
    <div id="response"></div>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();
        socket.on('message', function(message){
            console.log('process finished: ', message);
            document.getElementById("response").innerHTML = message;
        });
        document.getElementById("startButton").addEventListener("click", function(event) {
            var req = new XMLHttpRequest();
            req.onload = function() {
                console.log(req.response)
            };
            req.open("GET", "/start_process?socketId=" + socket.id);
            req.send();
        });
    </script>
  </body>
</html>