how to use node-webkit with express server?

自作多情 提交于 2019-12-10 04:19:14

问题


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 ?


回答1:


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




回答2:


Node-Webkit(means nodeJS+chromium framework) so you can run your server code(Express.js,etc) directly to your app(just install expressJS and then call directly client side code) that's it



来源:https://stackoverflow.com/questions/21826407/how-to-use-node-webkit-with-express-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!