如何在Express Server中使用Node-Webkit? [英] how to use node-webkit with express server?

查看:92
本文介绍了如何在Express Server中使用Node-Webkit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs和express开发一个应用程序.
我想将其导出为带有node-webkit的软件包.

如何启动服务器并使用它运行应用程序?

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 ?

推荐答案

我正在自己学习.这是将Express应用程序转换为Node Webkit应用程序的基础.

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

我将假设您有一个安装了两个模块的node.js应用程序.第一个是express.js,第二个是模板引擎.我正在使用把手,因此在本示例中将使用它.

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.

我还将假设您要转换的应用程序是最简单的应用程序,简而言之,我将假设您正在使用express做两件事-运行服务器并响应呈现视图文件的单个路由

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

第1步.

下载节点Webkit: http://nwjs.io/

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

第2步.

解压缩

第3步.

打开控制台并cd进入新创建的文件夹(我将从这里开始将此目录称为 app-parent ).在那里,请运行以下命令:

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

完成后,运行:

npm install express-handlebars

第4步:

app-parent 中创建另外两个文件夹.一个命名为资源,另一个命名为视图.同样在 app-parent 中创建一个名为 package.json 的文件.

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.

将以下代码复制到 package.json

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

第5步:

转到资源文件夹并创建一个名为 index.html 的文件.在此副本中,以下代码:

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>

第7步.

转到 app-parent 中的views文件夹,然后创建一个名为 index.hbs 的新文件.在此文件中,复制以下代码.

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>

最后一步.

app-parent 内部,单击名为 nw.exe 的文件.您的应用应该启动.

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

完成

这篇关于如何在Express Server中使用Node-Webkit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆