0

I tried to create an application with Node-Webkit and Sails.js. My API works fine, I get the JSON I need, but when integrated with Node-Webkit does not start the server.

My package.json contains:

{
   "name": "app-sails" 
   "main": "front/index.html" 
   "window": {
     "toolbar": true, 
     "width": 1024, 
     "height": 600, 
     "title": "Test Application" 
   }, 
   "scripts": {
     "start": "node server/app.js" 
   } 
}

The index.html is the main page you get when you use the generator angular.js yeoman and contains calls to the server that I have with sails.js. In the web running, but not with Node-Webkit.

When I run the .nw, I can see my index.html correctly; but without the data it throws the sails server.

I appreciate any help.

Devin
  • 7,690
  • 6
  • 39
  • 54

2 Answers2

3

I recently wrote a small article on this, you can check it out here: https://medium.com/unhandled-exception/sailsjs-and-node-webkit-4ccb8f810add

Basically you just need to execute sails using require as soon as the node-webkit window is loaded. This goes in your app.js file:

exports.onLoad = function() {
    var nwGUI = window.require('nw.gui');

    nwGUI.Window.get(window).on('loaded', function() {
        if (loaded) {
            return;
        }

        window.location.href = "http://localhost:1337/";
        loaded = true;
    });

    try {
        sails = require('sails');
    } catch (e) {
        console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.');
        console.error('To do that, run `npm install sails`');
        console.error('');
        console.error('Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.');
        console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,');
        console.error('but if it doesn\'t, the app will run with the global sails instead!');
        return;
    }

    // Try to get `rc` dependency
    try {
        rc = require('rc');
    } catch (e0) {
        try {
            rc = require('sails/node_modules/rc');
        } catch (e1) {
            console.error('Could not find dependency: `rc`.');
            console.error('Your `.sailsrc` file(s) will be ignored.');
            console.error('To resolve this, run:');
            console.error('npm install rc --save');
            rc = function() {
                return {};
            };
        }
    }

    // Start server
    sails.lift(rc('sails'));
}

and this on your .html entry point:

<html>
    <head>
        <title>Welcome!</title>
    </head>

    <body onload="process.mainModule.exports.onLoad();">
        <h1>hi!</h1>
    </body>
</html>

As you can see, most of that app.jss file is basically the same that comes out when you do a

sails new

I just wrapped everything up and executed that callback onload If you want you can always check out the article and the full github repo for a more detailed version of the code.

Oh! And don't forget to update your package.json and add:

{
      ...
      "node-main": "app.js",
      ...
}
AlterX
  • 2,131
  • 2
  • 14
  • 9
0

You should load the Sails-Page not your index.html.

One thing you could do is (in your HTML)

window.location.href = "http://localhost:1337/";
mdunisch
  • 3,627
  • 5
  • 25
  • 41
  • Ok, this directs to the sails Startpage. But I have to start the sails-app manually. Any idea how to start the sails-app when calling nw.exe (Windows). The "scripts"-Part in package.json doesn't work. Thanks. – liquid Oct 01 '14 at 05:50