【问题标题】:How do I use Sails.js in Node-Webkit?如何在 Node-Webkit 中使用 Sails.js?
【发布时间】:2014-10-23 22:23:34
【问题描述】:

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

我的 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 yeoman 时获得的主页,其中包含对我使用sails.js 的服务器的调用。在 web 上运行,但不是用 Node-Webkit。

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

感谢您的帮助。

【问题讨论】:

    标签: node.js angularjs sails.js node-webkit nedb


    【解决方案1】:

    我最近写了一篇关于这个的小文章,你可以在这里查看:https://medium.com/unhandled-exception/sailsjs-and-node-webkit-4ccb8f810add

    基本上,您只需要在 node-webkit 窗口加载后立即使用 require 执行sails。这在您的 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 文件与您执行 a 时输出的文件基本相同

    sails new
    

    我只是将所有内容都打包并执行了回调 onload 如果您愿意,可以随时查看文章和完整的 github 存储库以获取更详细的代码版本。

    哦!不要忘记更新你的 package.json 并添加:

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

    【讨论】:

      【解决方案2】:

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

      您可以做的一件事是(在您的 HTML 中)

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

      【讨论】:

      • 好的,这指向了sails Startpage。但我必须手动启动sails-app。知道如何在调用 nw.exe (Windows) 时启动sails-app。 package.json 中的“脚本”部分不起作用。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 2016-02-09
      • 2015-07-14
      • 2015-12-13
      相关资源
      最近更新 更多