我有一个这样的服务器:
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();
});
当我单击网页上的按钮时,将请求q_out_list.html。第二个app.get()具有exec(),它创建q_out_list.html文件。我检查了它是否在当前目录中创建了该文件。但我仍然遇到此错误。
答案 0 :(得分:0)
我认为此更改对您有所帮助:
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();
}
});