Node JS需要最初只能工作(调试)

时间:2016-03-17 12:13:04

标签: javascript html node.js sqlite require

我对如何正确使用Node JS需要感到困惑。

我有一个Node Webkit应用程序,它生成子窗口,每个子窗口都需要读取和写入数据到sqlite3数据库。

在我的index.html中,我有一个用于创建iframe的按钮

<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
</head>

<script>
   var sqlite3 = require('sqlite3').verbose();
   console.log(sqlite3);
</script>

<body>

   <!-- CREATE NEW APP IF NEEDED -->
   <button onclick="spawnApp()">Spawn App</button>

   <div id="containApp">

      <!-- RUN INITIAL APP -->
      <iframe src="apps/sub.html"></iframe>

   </div>

</body>
</html>

spawnApp()是index.js文件中的函数

function spawnApp() {

   var varIframe = document.createElement("iframe");
   varIframe.src = "apps/sub.html";

   var varContainer = document.getElementById("containApp");
   varContainer.appendChild(varIframe);

}

它产生的应用程序称为sub.html,它是与sqlite3建立数据库连接所必需的

<!DOCTYPE html>
<html>

<script>
   var sqlite3 = require('sqlite3').verbose();
   console.log(sqlite3);
</script>

<body>

   <h4>Sub App</h4>

</body>
</html>

当我第一次执行程序时,它似乎完美地工作,因为我在控制台中得到以下输出。

> Object                                                   index.html:9
> Object                                                     sub.html:6

这是我的预期,一个对象是从index.html上的require创建的,然后是初始的sub.html,它是一个静态写入index.html的iframe。

然而,我单击按钮添加新应用程序,我收到以下错误

> Object                                                   index.html:9
> Object                                                     sub.html:6
> Uncaught ReferenceError: require is not defined            sub.html:5

还有什么奇怪的是,如果我点击刷新按钮,即使没有使用按钮来创建新的应用程序,我也会收到类似的错误。

> Object {...}                                             index.html:9
> Uncaught ReferenceError: require is not defined            sub.html:5

我不知道造成这种情况的原因是什么,我花了很多时间试图解决这个问题,为什么只需要在初始程序启动时工作?

1 个答案:

答案 0 :(得分:0)

我相信我找到了解决这个问题的原因并找到了解决方法。

我正在阅读一些Node Q&amp; A论坛,似乎Node实际上并不支持iframe,因此调用节点模块时会出现一些意外行为。

虽然工作(可能不适合每个人的需要)只是简单地使用div而是使用一些jQuery / Javascript将sub.html中的元素写入div。

不是一个很好的解决方法,但我正在开发一个HTML生成器,以显着加快HTML内容的生成,这将使解决方法更有效。

相关问题