2014-09-23 29 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", 
     ... 
}