简体   繁体   中英

How to make Node.js backend JavaScript wait for my bash script to finish before doing anything else

Through a Node.js code, I am running 3 bash commands. Part of the file is shown below:

exec(str,
function(error, stdout, stderr){
    console.log('stdout:'+stdout);
    console.log('stderr:'+stderr);
    if(error!=null){
        console.log('exec error: '+error);
    }
exec('bash create_q_out_list.sh',
    function(error, stdout, stderr){
        console.log('stdout:'+stdout);
        console.log('stderr:'+stderr);
        if(error!=null){
            console.log('exec error: '+error);
        }
exec('bash replaceString.sh',
    function(error, stdout, stderr){
        console.log('stdout:'+stdout);
        console.log('stderr:'+stderr);
        if(error!=null){
            console.log('exec error: '+error);
        }
        });
    });
});

In the above code, 'bash replaceString.sh' generates an HTML file that I am displaying in an Iframe of my Home webpage. But, sometimes, before the new file is generated (ie 3rd bash command is finished), JS shows my old file on the Iframe. So, at the end even though my HTML file contains the correct content, old content has already been displayed.

Given below is the Iframe :

<iframe id='svg_frame' src="http://127.0.0.1:3000/render.html"></iframe>

Also, this is part of my server( render.html is the file that is being generated by the 3rd bash command):

app.get('/render.html', (req, res) =>{
  const rend = fs.readFileSync('./render.html');
  res.statusCode = 200;
  res.setHeader = ('Content-Type', 'text/html');
  res.write(rend);
  res.end();
});

I want the Node.js to wait for the new render.html file to be generated before showing anything on the Iframe.

Kinda agree with @Plixxer's statment, if it's a simple operation like replacing a string, just stick with JS on node, but regardless, I think your issue is in

app.get('/render.html', (req, res) =>{
  const rend = fs.readFileSync('./render.html');
  res.statusCode = 200;
  res.setHeader = ('Content-Type', 'text/html');
  res.write(rend);
  res.end();
});

specifically here const rend = fs.readFileSync('./render.html');

if that file exists BEFORE you replace stuff with your bash script, node will just get it from the file system and serve it to the client, it doesn't know that there is something going on in the background), that means you would have to do your replace within the app.get() block:


app.get('/render.html', (req, res) =>{
  let rend = fs.readFileSync('./render.html');
  rend = rend.replace('<regex or whatever you are replacing in bash>', '<replacment, can be a function as well!>');
  // or of course execute your bash replacement script here!
  //   exec('bash replaceString.sh', rend,
  //     function(error, stdout, stderr) {
  //       console.log('stdout:', stdout);
  //       console.log('stderr:', stderr);
  //       if ( error !== null ) {
  //           console.log('exec error:', error);
  //       }
  //     }
  //   );
  res.statusCode = 200;
  res.setHeader = ('Content-Type', 'text/html');
  res.write(rend);
  res.end();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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