如何将带有 html 文件的响应发送回 ejs 文件并在 ejs Node.js 上的 iframe 中显示

How to send back response with html file to ejs file and show in iframe on ejs Node.js

我是 Node.js 的新手,正在服务器上工作。我正在尝试将响应发送回我的输出文件 (html) 到 NodeJS 中的 ejs。我会在下面解释更多细节,

我有一个按钮,可以将 HTTP 请求 ('POST') 发送到 运行 python 文件中的 ejs,如下面的代码,

在 EJS 中,

<div class="form-group" style='margin-bottom:0px;'><div id='Button'><input type='button' class="form-control" value='Request' id="sendButton" onclick="save3()"/></div></div>
<form action="/saveText" method="POST" id="myForm"><input type="hidden" id="metrics_id" ng-repeat="obj in tags" value="{{obj.name}}" name="metrics_name"></form>

<script>
  function save3(){
    console.log(document.getElementsByName("metrics_name")[0].value);
    console.log(document.getElementsByName("metrics_name")[1].value);
    console.log(myForm.getElementsByTagName('input').length);
  }
  $(document).on('click', '#sendButton', function(){
    $('#myForm').submit();
  });
</script>

然后调用 python 脚本在 app.js(Node.js) 中使用 PythonShell

执行

Node.JS

app.post('/saveText', isLoggedIn, function(req, res){
  var pyshell = new PythonShell('/public/make_bokeh.py');
  pyshell.on('message', function(message){
    console.log(message);
  });
  pyshell.end(function(err){
    if(err){
      throw err;
    }
    console.log("Finished");
    res.sendFile(); ------------------------>I want to send output file back to EJS
  });
});

从 make_bokeh.py,它在 make_bokeh.py 所在的同一目录中创建输出 html 文件。

问题是我想用那个输出文件将 HTTP 响应发送回 ejs,然后将其放入 iframe。

是否可行,或者有什么想法?

你能做到吗:

节点JS

. . .
pyshell.end(function(err){
if(err){
  throw err;
}
console.log("Finished");
var HTMLFileGeneratedByPython = fs.readFileSync(path.join(__dirname, relPath));
  res.render('template.ejs', { htmlFile: HTMLFileGeneratedByPython });
});

EJS

<!-- HTML HEADER -->
<body>
  <%= htmlFile %>
</body>
</html>