2014-09-23 63 views
0

我试图用Node-Webkit和Sails.js创建一个应用程序。我的API工作正常,我得到了我需要的JSON,但是当与Node-Webkit集成时不会启动服务器。如何在Node-Webkit中使用Sails.js?

我的package.json包含:

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

index.html是你的主网页,当你用发电机angular.js自耕农和包含对我有sails.js调用服务器。在网页中运行,但不在Node-Webkit中运行。

当我运行.nw时,我可以正确看到我的index.html;但没有数据会抛出sails服务器。

我很感激任何帮助。

回答

0

您应该加载Sails-Page而不是您的index.html。

有一两件事你可以做的是(在你的HTML)

window.location.href = "http://localhost:1337/"; 
+0

好吧,这引导到帆首页中国。但我必须手动启动sails-app。任何想法如何在调用nw.exe(Windows)时启动sails-app。 package.json中的“脚本”部分不起作用。谢谢。 – liquid 2014-10-01 05:50:01

3

我最近写了一篇关于这一篇小文章,您可以点击此处查看:https://medium.com/unhandled-exception/sailsjs-and-node-webkit-4ccb8f810add

基本上你只需要执行帆在加载node-webkit窗口后立即使用require。这正好在app.js文件:

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')); 
} 

,这对你的.html切入点:

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

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

正如你所看到的,大多数app.jss文件基本上是来自相同出来的时候,你做了

sails new 

我刚刚结束了一切,如果你愿意,你可以随时检查出更详细的VERSI文章和完整的GitHub库执行的回调onload事件 的代码。

哦!而且不要忘了更新您的package.json,并添加:

{ 
     ... 
     "node-main": "app.js", 
     ... 
}