0

我正在尝试使用NeDB数据库来持久化应用程序数据。我正在尝试使用以下方法连接到我的数据库,如下所示:

var Datastore = require('nedb')
  , path = require('path')
  , db = new Datastore({ filename: path.join(require('nw.gui').App.dataPath, 'something.db') }

但不幸的是,它失败了,因为这只适用<script></script>于 html 文件中带有标签的客户端代码。我怎么能在服务器端做同样的事情?

4

1 回答 1

1

这里的问题是,你不能在 nodejs 上下文中 require('nw.gui') ,因为在 nodejs 环境中没有窗口。没有窗口,没有gui。所以你可以做的,就是script在你的主文件(index.html)中创建一个标签,并将src添加到你的db.js文件中,它应该可以正常工作。

在 index.html 中

<script src="db/db.js"></script>

在 db/db.js

var Datastore = require('nedb')
,   path = require('path')
,   dbFile = path.join(require('nw.gui').App.dataPath, 'something.db')
,   db = new Datastore({ filename: dbFile })
于 2015-08-19T14:20:40.043 回答