我尝试用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
自定义时获得的主页面,并且包含对sails.js
的服务器的调用。在Web上运行,但没有使用Node-Webkit。
当我运行 .nw 时,我可以正确地看到我的index.html
;但没有数据就会抛出sails服务器。
我感谢任何帮助。
答案 0 :(得分:3)
我最近写了一篇关于此的小文章,你可以在这里查看: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文件基本上与您执行时相同
sails new
我只是将所有内容都包装好并执行了回调onload 如果您愿意,可以随时查看文章和完整的github repo,以获取更详细的代码版本。
哦!并且不要忘记更新你的package.json并添加:
{
...
"node-main": "app.js",
...
}
答案 1 :(得分:0)
您应该加载Sails-Page而不是index.html。
您可以做的一件事是(在您的HTML中)
window.location.href = "http://localhost:1337/";