我最近写了一篇关于这个的小文章,你可以在这里查看: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 存储库以获取更详细的代码版本。
哦!并且不要忘记更新您的 package.json 并添加:
{
...
"node-main": "app.js",
...
}