Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use node-webkit with express server?

I am developing an app using nodejs and express.
I want to export it as a package with node-webkit.

How can I start server and run app with it ?

like image 209
Amb Avatar asked Feb 17 '14 10:02

Amb


1 Answers

I am working on learning this myself. Here are the basics for converting an express app to a node webkit app.

I will assume that you have a node.js app with two modules installed. The first being express.js and the second some template engine. I am using handlebars so I will use it for this example.

I will also assume the app you want to convert is the simplest one possible, in short I will assume you are using express to do two things - run a server and respond to a single route that renders a view file

Step 1.

Download node webkit: http://nwjs.io/

Step 2.

Unzip it

Step 3.

Open up the console and cd into the newly created folder (I will call this directory app-parent from here on out). Once you are there - run this command:

npm install express

When this is done run:

npm install express-handlebars

Step 4:

In app-parent create two additional folders. One named resources and the other named views. Also in app-parent create a file called package.json.

Copy the following code into package.json

{
    "name": "app",
    "main": "resources/index.html"
}

Step 5:

Go to the resources folder and create a file named index.html. Inside of this copy the following code:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>

</head>
<!--______________________________________________________BEGIN APP-->

<body>
    <script>
    </script>
    <script>
    var express = require('express');
    var app = express();

    var expressHbs = require('express-handlebars');

    app.engine('hbs', expressHbs({
        extname: 'hbs'
    }));

    app.set('view engine', 'hbs');


    app.get("/", function(req, res) {
        res.render("index", {
            item: "weeeeeeeee"
        })
    })



    app.listen("3000", function(err) {

        if (err) {
            console.log("server is not working");
        } else {
            console.log("Server is working on 3000");
        }
    })



    window.location.href = 'http://localhost:3000';
    </script>
</body>
<!--______________________________________________________END APP-->

</html>

Step 7.

Go to the views folder in app-parent and create a new file called index.hbs. Inside this file copy the following code.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>
    <!--______________________________________________________BEGIN APP-->
    <body>
        <p>Oink</p>
        {{item}}
    </body>
    <!--______________________________________________________END APP-->
</html>

Final step.

Inside app-parent click the file named nw.exe. Your app should launch.

DONE

like image 66
William Avatar answered Oct 09 '22 10:10

William