问题
I have developed a Meteor app. I would like to package this app in the node-webkit app runtime for Chromium. I need the Meteor server process to run locally. How would I go about starting the Meteor server process when a user launches the node-webkit app?
I know I can start a NodeJS server instance with node-webkit like this:
server.js
#!/usr/bin/env node
require('http').createServer(function(req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end('<h1>sup</h1>');
}).listen(9000, '127.0.0.1');
Then if I run:
$ nw ./
node-webkit will start the NodeJS server and launch the node-webkit instance. I'm not including the package.json file here, but it essentially just says look at http://127.0.0.1:9000
.
So, how would I go about writing that server.js
file to start the Meteor instance while the node-wekkit app is running?
Thanks for any thoughts.
回答1:
First Bundle your meteor app meteor build --directory /your/node-webkit/project/
and use this code to start your app. But, packaging Meteor with node-webkit can be a little bit more complicated. First, you'll need a mongodb server running on your client computer or somewhere the client can connect anytime.
var path = require('path');
var child_process = require('child_process');
// change these
var PORT = 9000;
var ROOT_URL = 'http://localhost:'+PORT;
var MONGO_URL = 'mongodb://localhost:27017/my_app_db';
var NODE_BIN = '/usr/local/bin/node';
// install npm dependencies
var options = {cwd: path.resolve(__dirname, 'bundle/programs/server/')};
var installNpm = child_process.exec('npm install', options, onNpmInstall);
function onNpmInstall (err, stderr, stdout) {
if(err) throw new Error('could not install npm dependencies');
// start Meteor
var options = {
env: {PORT: PORT, MONGO_URL: MONGO_URL, ROOT_URL: ROOT_URL},
cwd: __dirname
};
var proc = child_process.spawn(NODE_BIN, ['bundle/main.js'], options);
proc.on('close', function (code) {
console.log('Meteor exited with code ' + code);
});
}
You must remove mongo related smart packages if you want a 100% client-side application.
来源:https://stackoverflow.com/questions/24898467/how-can-i-start-a-meteor-instance-before-launching-a-node-webkit