简体   繁体   中英

How to bundle an express server and html script into same Node Webkit application?

I am having an extremely difficult time with trying to turn my current Node.js application into a desktop app using Node Webkit. Without using webkit, my application works perfectly. I start my server using "Node app.js" in the terminal. Then, when I connect to localhost:8080, the client connects and the index.html page is loaded; the app then works perfectly. I need all this to happen from a desktop app and am thus using Node webkit.

I can't figure out how to get all this to happen using Node webkit. I have searched online for hours and this seems to be a common problem but no one has a decent and easy-to-follow solution. If someone could please help me it would be greatly appreciated.

Also on a side note, I have tried loading my node.js file first by using the "node-main" property in my package.json file, but this does not work. For some reason, the "node-main" property crashes my webkit app every single time I try to use it.

If anyone can provide a clear walkthrough of how to implement all this into a single node webkit app that would be very helpful. Thanks!

you need to use the node-main to do what you are trying to achieve. Your code probably crash due to exception.

first, add the node-main to the config

"node-main": "index.js",

To debug the code, use attach uncaught exception handler:

var report_error = function(err){
    if (typeof console == 'object' && typeof console.log == 'function') {
        console.log('Exception: ' + err.message, err.stack);
    } else {
        setTimeout(function(){report_error(err);},200);
    }

};
process.on('uncaughtException', function (err) {
    report_error(err);
});

note that I check if the console is accessible - when this script run console is not yet accessible:

This symbol is not available at the time the script is loaded, because the script is executed before the DOM window load ( source ).

I experienced similar issues because I used console.log in the index.js. after using the following function instead of console.log I got it to work:

var console_log = function(err){
    if (typeof console == 'object' && typeof console.log == 'function') {
        console.log(err);
    } else {
        setTimeout(function(){console_log(err);},200);
    }
};

is a really weird case for a desktop app, but if you really need that, you could run your server using a child process first from a window.onload method included in the index.html of your NW app, with some code like this:

var exec = require('child_process').exec;
exec('node app.js ', function(error, stdout, stderr) {
    if (error !== null) {          
      throw error;
    }

    console.log('Server listening');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM