简体   繁体   中英

NodeJS : Cannot execute shell script synchronously

I am trying to execute a shell command and return the output to the webpage

  app.post("/api/procText", function (request, response) {
      var input = request.body.inString;
      console.log(request.body)
      var exec = require('child_process').execSync;
      var cmd = 'scripts/myScript -s' + '\'' + input + '\'';
      var script_output = ''
      exec(cmd, {
          env: {
              'resources': '/dasd/okok/data'
          }
      }, function (error, stdout, stderr) {
          console.log("Inside exec " + stdout)
          script_output = stdout
      });
      console.log("Outside " + script_output);
      response.send("Sending " + script_output);
  });

However, the script_output is always blank and also i do not see the inner message on the console

What am i doing wrong?

Also, here is the corresponding html

<body>
    <div class="container">
        <h1>Welcome.</h1>
        <div id="sampleInput" class="input-group-lg center-block helloInput">
            <p class="lead">Input Text?</p>
            <input id="sample_text" type="text" class="form-control"
                placeholder="inString" aria-describedby="sizing-addon1" value="" />
        </div>
        <p id="response" class="lead text-center"></p>


    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script
        src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script>
        //Submit data when enter key is pressed
        $('#sample_text').keydown(function(e) {
            var inString = $('#sample_text').val();
            if (e.which == 13 && inString.length > 0) { //catch Enter key
                //POST request to API to create a new visitor entry in the database
                $.ajax({
                    method : "POST",
                    url : "./api/procText",
                    contentType : "application/json",
                    data : JSON.stringify({
                        inString : inString
                    })
                }).done(function(data) {
                    alert(data);
                    $('#response').html(data);
                    $('#sampleInput').hide();

                });
            }
        });
    </script>
</body>

i figured it out. all i had to do was return stdout using callback

script_output=exec(cmd, {env: {'NLTK_DATA': process.cwd()+"/scripts/nltk_data"}}, function(error, stdout, stderr) {
    console.log("Inside exec "+stdout);
    return stdout;
});
  console.log("Sending "+script_output);
response.send(script_output);

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