0

I have a server like this:

const fs= require('fs');
const express = require('express');
const app = express();
const hostname = '127.0.0.1'; // Local host ip.
const homebase = fs.readFileSync('./with_radio_final.html');

var exec = require('child_process').exec, child;
express.static('.');
var sub="";
var obj="";
var pred="";
var type="";
app.use(express.static('.'));


app.get('/',(req, res) => {
   sub=req.query['Subject'];
   pred=req.query['Predicate'];
   obj=req.query['ObjecT'];
   type=req.query['type'];

   if(sub != undefined){
    console.log("SUBJECT= "+sub);
    console.log("PREDICATE= "+pred);
    console.log("OBJECT= "+obj);
    console.log("TYPE= "+type);
    }
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.write(homebase);
  res.end();
});

app.get('/q_out_list.html', (req, res) =>{
  console.log("In q_out");
  if(sub!=undefined)
  {
    if(type==='Search')
            str = './search "<'+sub+','+pred+','+obj+'>" > temp';
    if(type==='Create')
            str = './create "<'+sub+','+pred+','+obj+'>" > temp';
    if(type==='Delete')
            str = './delete "<'+sub+','+pred+','+obj+'>" > temp';
    //console.log(str);
    //The exec() creates a file q_out_list.html
     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);
              }
        });
     });
  }
  const q_out = fs.readFileSync('./q_out_list.html');
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.write(q_out);
  res.end();
});

When I click on a button on my web-page, q_out_list.html is requested. The second app.get() has exec() which creates the q_out_list.html file. I have checked that it does create this file in the current directory. But I am still getting this error.

Vipin Baswan
  • 37
  • 1
  • 8

1 Answers1

0

i think this change help to you:

const fs= require('fs');
const express = require('express');
const app = express();
const hostname = '127.0.0.1'; // Local host ip.
const homebase = fs.readFileSync('./with_radio_final.html');

var execSync = require('child_process').execSync, child;
express.static('.');
var sub="";
var obj="";
var pred="";
var type="";
app.use(express.static('.'));


app.get('/',(req, res) => {
   sub=req.query['Subject'];
   pred=req.query['Predicate'];
   obj=req.query['ObjecT'];
   type=req.query['type'];

   if(sub != undefined){
    console.log("SUBJECT= "+sub);
    console.log("PREDICATE= "+pred);
    console.log("OBJECT= "+obj);
    console.log("TYPE= "+type);
    }
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.write(homebase);
  res.end();
});

app.get('/q_out_list.html', (req, res) =>{
  console.log("In q_out");
  if(sub!=undefined)
  {
    if(type==='Search')
            str = './search "<'+sub+','+pred+','+obj+'>" > temp';
    if(type==='Create')
            str = './create "<'+sub+','+pred+','+obj+'>" > temp';
    if(type==='Delete')
            str = './delete "<'+sub+','+pred+','+obj+'>" > temp';
    //console.log(str);
    //The exec() creates a file q_out_list.html

     const stdout1 = execSync(str, {cwd: str});
     const stdout2 = execSync('bash create_q_out_list.sh');
     const q_out = fs.readFileSync('./q_out_list.html');
     res.statusCode = 200;
     res.setHeader('Content-Type', 'text/html');
     res.write(q_out);
     res.end();
  }
});
mohammad javad ahmadi
  • 2,031
  • 1
  • 10
  • 27
  • The initial error got removed. But another error popped on line: const stdout1=execSync(...). It says spawnSync /bin/sh ENOENT – Vipin Baswan Jul 06 '19 at 11:17
  • ok! and very good, but you can use this link in stack over flow for solving your new problem https://stackoverflow.com/questions/39754168/bin-sh-not-found-spawnsync – mohammad javad ahmadi Jul 06 '19 at 11:35
  • Ok. I removed this error. But can you tell me how to force JS to execute my bash command before jumping to the next statement. Its happening quite a few times with me. I have a file which is generated by a bash command. Each time, JS picks up my old file and displays it without considering the bash command – Vipin Baswan Jul 06 '19 at 11:39