Skip to content

NW.js use express and similar web frameworks

holodny edited this page Jul 12, 2015 · 6 revisions

Before develop an express base NW.js(node-webkit) APP, you should see About Node.js server side script in nw.js, it provides some good ways to replace those framework.

But if you already have an express base webapp, you want to use NW.js make it use as desktop app, there has problmes.

First, express provide a web server and don't have index.html file, so what should I do at package.json main property?

The solution is you can create a index.html file, and use the follow code to run the system:

<script type="text/javascript" src="app.js"></script>

In NW.js you can use node modules in html files, more details on this see: Using Node modules

Second, express 3/4 use the ./bin/www to start a web server, if you run the app.js it will fail to start. And sometimes if you want run a node app directly not through a html file, your can use the follow way:

We can setup node-main at package.json, node-main property is a command which will be call when NW.js app start, more details see: Node main. And for express case, we should also setup the main property to http://localhost:3000. If we setup a filename to that, NW.js will open it with file protocol, so you will see the source code, if you setup a url with http protocol, NW.js will open it just like a browser.

"node-main": "./bin/www",
"main": "http://localhost:3000"

Put the above code in your package.json and run it. It seems like OK but there have a problem (maybe a bug), is that NW.js show a blank page and you must refresh the page then the content will show. There have a way to solve this problem, you can create a html file and write JavaScript code location.href="http://localhost:3000/" in it, then setup the main as your-dir-html.html.

If the application that you're working with doesn't contain a www binary, try something like this:

"node-main": "./app.js",
"main": "http://localhost:3000"

This worked with the mean-stack-relational application (https://github.com/jpotts18/mean-stack-relational) which doesn't have a www binary.

Clone this wiki locally