Search code examples
node.jsnode-webkitnedb

How to create database within nodejs application code in node-webkit?


I'm trying to use NeDB database for persistence of application data. I'm trying to use the following approach to connect to my database like the following:

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

But unfortunatly it fails because this works only client code withit <script></script> tags in html file. How could I do same thing on server side?


Solution

  • The problem here is, that you cannot require('nw.gui') in the nodejs context, because in the nodejs environment is no window. No window, no gui. So what you can do, is just create a script tag in your main file (index.html) with the src to your db.js file with the above content, it should work fine.

    in index.html

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

    in 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 })